1. 启用 Annotaion 配置:
<?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-4.3.xsd">
<context:annotation-config/>
</beans>
2. 设置组件扫描与 Bean 命名
@Repository, @Service, and @Controller,@Component 这 四 个 Annotation 功 能 相 同(@Repository 声明 Dao,@Service 声明 Service,@controller 声明控制器),都是用在类上的 Annotation,说明让 spring 实例化此类的对像,并放入 spring 容器中
@componet(“id”)其中 id 声明 bean 对像的名字
3. 设置组件扫描的基础包
@Configuration
@ComponentScan(“基包名”)
Public class AppConfig{}
@Configuration
@ComponentScan(basepackages=“基包名”)
Public class AppConfig{}
@Configuration
@ComponentScan(basepackages={“基包名”,”...”})
Public class AppConfig{}
@Configuration
@ComponentScan(basePackageClasses={App1Config.class,App2Config.class})
Public class AppConfig{}
以上 App1Config 与 App2Config 所在的包作为组件扫描的基础包
@Configuration
@ComponentScan(basePackages = "org.example",
includeFilters = @Filter(type = FilterType.REGEX, pattern =
".*Stub.*Repository"), excludeFilters = @Filter(Repository.class))
public class AppConfig {... }
<beans>
<context:component-scan base-package="org.example">
<context:include-filter type="regex" expression=".*Stub.*Repository"/>
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>
4. Annotation 自动装配
@Autowired 自动装配和 JSR 330’s @Inject 对应,可用在构造方法、属性 setter 方法,有属性@Autowired(required=false)
@qualifiers 注明要装配 bean 的标识,用于多个 bean 无法确定装配哪个的情况。
<bean id="teacher1" class="com.bean.Teacher"/>
<bean id="teacher2" class="com.bean.Teacher">
<property name="name" value="老刘"/>
<property name="age" value="35"/>
</bean>
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"/ApplicationContext.xml"})
public class MyTest {
@Autowired
@Qualifier("teacher2")
Teacher teacher;
@Test
public void test(){
System.out.println(teacher);
}
}
也可以用 Java 注解来完成自动装配 @Named 和@Inject
@Named
public class Teacher {
private String name;
private int age;
public Teacher() {
this.name = "王老师";
this.age = 38;
}
/*getter setter */
}
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"/ApplicationContext.xml"})
public class MyTest {
@Inject
Teacher teacher;
@Test
public void test(){
System.out.println(teacher);
}
}
@Primary 用于声明 bean 的首先,用在多个 bean,无法选择装配谁的情况
<bean id="teacher1" class="com.bean.Teacher" primary="true"/>
@Bean
@Primary
public Student getStudent(){
Student student = new Student();
student.setName("张三");
student.setAge(18);
return student;
}
5. Bean 的初始化方法与销毁方法
方法一: @PostConstruct and @PreDestroy
@Component
public class Teacher {
private String name;
private int age;
public Teacher() {
this.name = "王老师";
this.age = 38;
System.out.println("构造方法。。。。。");
}
@PostConstruct
public void init(){
System.out.println("init........");
}
@PreDestroy
public void destroy(){
System.out.println("init........");
}
/*getter toString setter */
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value={"/ApplicationContext.xml"})
public class MyTest {
@Inject
Teacher teacher;
@Test
public void test(){
System.out.println(teacher);
}
}
执行结果:
方法二:XML
<bean id="teacher1" class="com.bean.Teacher" init-method="init" destroy-method="destroy"/>
方法三:java 显式配置
@Configuration
@ComponentScan(basePackages = {"com"})
public class Config {
@Bean(initMethod = "init" ,destroyMethod = "destroy")
public Teacher teacher(){
return new Teacher();
}
}
6. Spring 和 JSR 的 Annotaion 对比