基于注释注入@Component @Value @Resource @Autowired @PostConstruct @PreDestroy等
【Student 】
package com.athl.spring;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/*
与@Component具有相同功能,不同意义的注解还有三个:
@Repository:注释在Dao实现类上
@Service:注解在Service实现类上
@Controller:注解在处理器上(SpringMVC)
*/
@Component("myStudent")
public class Student {
@Value("张三")
private String name;
@Value("22")
private int age;
/*域属性的byType自动注入,只需@Autowired*/
//@Autowired
/*byName自动注入,需要@Qualifier与@Autowired配合使用*/
//@Qualifier("mySchool")
/*@Resource是jdk的注解,版本6及以上版本.*/
//@Resource/*byType自动注入*/
@Resource(name="mySchool")/*byName自动注入*/
private School school;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSchool(School school) {
this.school = school;
}
/*Bean生命周期始末注解*/
@PostConstruct
public void afterInit(){
System.out.println("初始化完毕后");
}
@PreDestroy /*需要显示关闭容器*/
public void preDestroy(){
System.out.println("销毁之前");
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school
+ "]";
}
}
【School 】
package com.athl.spring;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("mySchool")
public class School {
@Value("黄埔军校")
private String name;
public School() {
}
public School(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "School [name=" + name + "]";
}
}
【applicationContext.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">
<!-- 扫描com.athl这个包及其子包 -->
<!-- <context:component-scan base-package="com.athl"/> -->
<!-- 扫描com.athl的所有子包 -->
<!-- <context:component-scan base-package="com.athl.*"/> -->
<context:component-scan base-package="com.athl.spring"/>
<!-- 注解与xml同时使用 -->
<!--
注解好处:配置方便,直观;
缺点:修改需要重新编译代码。
xml好处:修改无须重新编译。
同时使用,xml的优先级要高于注解.若要修改修改配置文件即可。
那么Bean类要有setter或构造器.
-->
</beans>
【Test】
package com.athl.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void run(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ac.getBean("myStudent"));
/*显示关闭容器*/
((ClassPathXmlApplicationContext)ac).close();
}
}