spring 注解
1.Required (在编译时检查作用,不会给你注入)
This annotation simply indicates that the affected bean property must be populated at configuration time
也就是说:需要注入的类在配置的就得配置好,Required只是检查需要注入的类又没有注入,没有就会出错
例如配置文件这样:
<bean id="u" class="com.spring.dao.impl.UserDAOImpl"></bean> <bean id="userService" class="com.spring.service.UserService"> <!-- <property name="userDAO" ref="u"></property> 需要注入一个参数--> </bean>
这时如果你要是这样启动服务:就会报错
@Required //检查是否注入 public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; }
但是如果这样:就不会报错,应该autowired 先给程序注入,required再去检查是否成功注入
@Required //检查是否注入 @Autowired //注入 public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; }
2.Autowired(自动注入) 默认是按照类型注入
<1>如上例中的配置文件,调用服务时只需要这样调用
@Autowired public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; }
<2>如果配置中存在两个相同类型的bean如:
<bean id="u" class="com.spring.dao.impl.UserDAOImpl"></bean> <bean id="u2" class="com.spring.dao.impl.UserDAOImpl"></bean> <bean id="userService" class="com.spring.service.UserService"> <!-- <property name="userDAO" ref="u"></property> 需要注入一个参数--> </bean>
这个时候如果单独用上面的配置是会报错的,因为配置中有两个相同类型的bean,这时我们要指定bean的标志(bean的名字)
@Required //j检测是否注入 @Autowired //注入 //Qualifier 指定name的bean public void setUserDAO(@Qualifier(value="u") UserDAO userDAO) { this.userDAO = userDAO; }
<3>Autowired 还可以这样用@Autowired(@required=false) 表示不需要自动装载
<4>autowired 可以写在setter方法,一般方法,属性上,还可以注入数据类型,map类型等,参考API文档
3.Qualifier和Autowired结合
4.Resource指定注入的bean 默认先按名称注入,没有就按类型(import javax.annotation.Resource)需要导入common-annotations.jar
<bean id="userDAO" class="com.spring.dao.impl.UserDAOImpl"></bean> <bean id="u2" class="com.spring.dao.impl.UserDAOImpl"></bean> <bean id="userService" class="com.spring.service.UserService"> <!-- <property name="userDAO" ref="u"></property> 需要注入一个参数--> </bean>
业务调用:
@Resource//默认按userDAO找配置 public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; }
也可以这样:
@Resource(name="u2") //指定名字 public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; }
5.Component 不用再xml中配置每个bean,只需要在配置文件中配置
<!--利用注解形式,并声明初始化容器时扫描的包 --> <context:annotation-config/> <context:component-scan base-package="com.spring"> </context:component-scan>
在类中:
@Component("userDAO") //自动将类生成对象加到容器中,默认时key是userDAOImpl public class UserDAOImpl implements UserDAO { @Override public void add(User user) { // TODO Auto-generated method stub System.out.println("user added successful"); } }
@Component public class UserService { private UserDAO userDAO; public UserDAO getUserDao() { return userDAO; } @Resource public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void add(User user) { userDAO.add(user); } }
测试类
@Test public void jDomTest() throws JDOMException, IOException { ApplicationContext context=new ClassPathXmlApplicationContext("classpath*:beans.xml"); UserService service=(UserService)context.getBean("userService"); service.add(new User()); }
说明:beans.xml配置文件指定扫描com.spring 包下的所有类,并将所有在类头加油“Component”的类都生成对象放到集合中
<context:annotation-config/>
<context:component-scan base-package="com.spring">
</context:component-scan>