Spring----基于注解的方式装配
在之前
需要生成一个spring-context.xml,来配置bean
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="springContext" class="com.zy.myshop.comment.until.MyApplicationContext" />
<bean id="getuserImpl" class="com.zy.myshop.dao.impl.GetUserImpl"></bean>
<bean id="userLoginimpl" class="com.zy.myshop.service.impl.UserLoginImpl"></bean>
</beans>
对于 DI 使用注解,将不再需要在 Spring 配置文件中声明 Bean 实例。Spring 中使用注解, 需要在原有 Spring 运行环境基础上再做一些改变
需要在 Spring 配置文件中配置组件扫描器,用于在指定的基本包中扫描注解。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="springContext" class="com.zy.myshop.comment.until.MyApplicationContext" /> //好像必须加上
<context:annotation-config />
<context:component-scan base-package="com.zy.myshop"/>
</beans>
注解:
@Component: 需要在类上使用注解 @Component,该注解的 value 属性用于指定该 bean 的 id 值。(像一般工具类等的注解)
@Repository: 用于对 DAO 实现类进行注解 (数据访问层)
@Service : 用于对 Service 实现类进行注解 (逻辑业务层)
@Controller: 用于对 Controller 实现类进行注解 (视图层)
使用
MyapplicationContext
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//public class MyApplicationContext {
// public static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-context.xml");;
//}
public class MyApplicationContext implements ApplicationContextAware, DisposableBean {
public static final Logger logger = LoggerFactory.getLogger(MyApplicationContext.class);
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
assertContextInjected();
return applicationContext;
}
public void destroy() throws Exception {
logger.debug("清空 applicationContext");
System.out.println("清空application.....");
applicationContext = null;
}
public static <T> T getBean(String name) {
assertContextInjected();
logger.info("getbean");
System.out.println("getBean........");
return (T) applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
assertContextInjected();
return applicationContext.getBean(clazz);
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
logger.info("加载 application");
System.out.println("加载application");
MyApplicationContext.applicationContext = applicationContext;
}
private static void assertContextInjected() {
Validate.validState(applicationContext != null, "applicationContext 属性未注入,请在 spring-context.xml 配置中定义 SpringContext");
}
}
在托管的类上使用注解
注解与 XML 配置的区别
注解的好处是,配置方便,直观。但其弊端也显而易见:以硬编码的方式写入到了 Java 代码中,其修改是需要重新编译代码的。
XML 配置方式的最大好处是,对其所做修改,无需编译代码,只需重启服务器即可将新的配置加载。
若注解与 XML 同用,XML 的优先级要高于注解。这样做的好处是,需要对某个 Bean 做修改,只需修改配置文件即可。
补充
@Scope
需要在类上使用注解 @Scope,其 value 属性用于指定作用域。默认为 singleton。
@Value
需要在属性上使用注解 @Value,该注解的 value 属性用于指定要注入的值。
@Autowired
需要在域属性上使用注解 @Autowired,该注解默认使用 按类型自动装配 Bean 的方式。
使用该注解完成属性注入时,类中无需 setter。当然,若属性有 setter,则也可将其加到 setter 上。
@Resource
需要在域属性上使用注解 @Resource,该注解有一个 name 属性,可以创建指定的 bean
@Resource(name = "userService") private UserService userService;
@PostConstruct
在方法上使用 @PostConstruct 相当于初始化

