Spring 常用注入注解(annotation)和其对应xml标签
使用注解需要修改bean.xml:
<?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"> <context:annotation-config/> <bean class="example.SimpleMovieCatalog"> <qualifier value="main"/> <!-- inject any dependencies required by this bean --> </bean> <bean class="example.SimpleMovieCatalog"> <qualifier value="action"/> <!-- inject any dependencies required by this bean --> </bean> <bean id="movieRecommender" class="example.MovieRecommender"/> </beans>
黄色部分内容会使spring加载AutowiredAnnotationBeanPostProcessor等可以识别注解的bean。
@AutoWire,自动注入,一般放在属性的set方法上,会为该属性自动注入。默认的注入是使用byType,就是根据xml中bean的类型去匹配。
可以和@Qualifier匹配使用。
注解实现方式:
package com.bjsxt.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
public class UserService {
private UserDAO userDAO;
public void init() {
System.out.println("init");
}
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Autowired
public void setUserDAO(@Qualifier("u") UserDAO userDAO) {//Qualifier:根据qualifier标签去查找,也可以根据name或者id去查找匹配的bean
this.userDAO = userDAO;
}
public void destroy() {
System.out.println("destroy");
}
}
对应的xml实现方式:
<?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-2.5.xsd" >
<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> <property name="daoId" value="1"></property> </bean> <bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl"> <property name="daoId" value="2"></property> </bean> <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byType"><!-- 也可以设在beans标签上,好像是default-autowire scope="prototype"表示每次都创建一个新的实例对应的有singleton--只有一个实例 --> </bean>
</beans>
@Resource
此标签也用于注入属性。相对来说用的比较多。
该标签需要导入J2EE的JAR包:common-annotations.jar
javax.annotation.Resource,默认byName,如果找不到对应name会去找对应type
@Resource
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Resource(name=”uuu”) //可以byName
以上两种注解虽然可以在代码中设置注入,但是还要求在xml文件中配置bean。
Component可以省去。
5.10.3 Using filters to customize scanning
By default, classes annotated with @Component
, @Repository
, @Service
, @Controller
, or a custom annotation that itself is annotated with @Component
are the only detected candidate components. However, you can modify and extend this behavior simply by applying custom filters. Add them as include-filter or exclude-filter sub-elements of thecomponent-scan
element. Each filter element requires the type
and expression
attributes. The following table describes the filtering options.
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.bjsxt"/>
</beans>
scan:扫描。表示扫描某个包和其自包,将能作为组件(添加了@Component)的内容解析出来。
package com.bjsxt.dao.impl;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("u") //这样写表示这个类是一个组件,在另外一个类看来它就是一个资源。不指定它的key默认会是userDAOImpl(即类名首字母改成小写)
public class UserDAOImpl implements UserDAO {
public void save(User user) {
//Hibernate
//JDBC
//XML
//NetWork
System.out.println("user saved!");
}
}
package com.bjsxt.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("userService")
public class UserService {
private UserDAO userDAO;
public void init() {
System.out.println("init");
}
public void add(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="u")
public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destroy() {
System.out.println("destroy");
}
}
额外的一些内容:
<bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype">
scope对应的注解是@Scope(“”)
init-method对应的注解是@PostConstruct,表示构造完成之后再执行它注解的方法。
destroy-method对应的注解是@PreDestroy,容器销毁之前。