依赖注入的注解
支持的注解
@Resource
@AutoWired
@Component
@PostConstruct
@PreDestroy
注解扫描器
导入命名空间
启动类扫描的注解解析器
启动依赖注入的注解解析器
<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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
<!--
component指的是bean
base-package 可以指定路径的包或者类解析
-->
<context:component-scan base-package="com.lzl.test.di.scan"></context:component-scan>
</beans>
Java文件
Person类
@Component 定义在类上,注解会自动扫描并解析该类为对象
@Component
public class Person {
@Resource(name="student")
private Student stu;
public Person(){
System.out.println("=Person 构造函数=");
}
public void sayPerson(){
this.stu.sayStu();
}
}
Student类
@PostConstruct 定义在方法上,在构造函数执行之后,由IOC自动执行
@PreDestroy 定义在方法上,由IOC容器close之前执行。
@Component
public class Student {
public Student(){
System.out.println("=构造函数=");
}
public void sayStu(){
System.out.println("=hello Stu!=");
}
@PostConstruct //构造方法执行后,执行
public void init(){
System.out.println("init student");
}
@PreDestroy //类销毁之前,执行
public void Destory(){
System.out.println("destory student");
}
}
测试类
public class PersonTest{
static String path=null;
static{
path="com/lzl/test/di/scan/applicationContext.xml";
}
@Test
public void test(){
ClassPathXmlApplicationContext cpac = new ClassPathXmlApplicationContext(path);
Person p = (Person) cpac.getBean("person");
p.sayPerson();
cpac.close();
}
}
总结
使用依赖注入注解解析器执行的过程
- 启动spring容器
- 启动注解扫描器
- 启动依赖注入的注解解析器
- 如果类中含有@Component注解的value值为空。就会默认将类识别为一下格式
<bean id="Person" class="com.lzl.text.di.scan.Person"></bean>
- 如果累中@Component注解的value值为”aaa”。就会默认识别为
<bean id="aaa" class="com.lzl.text.di.scan.Person"></bean>
使用注解实现MVC框架
需要了解:
@Component是所有受Spring管理组件的通用形式; 而@Repository、@Service和 @Controller则是@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说, 你能用@Component来注解你的组件类, 但如果用@Repository、@Service 或@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。
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-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
1. 导入命名空间
2. 启动类扫描的注解解析器
3. 启动依赖注入的注解解析器
-->
<context:annotation-config></context:annotation-config>
<!--
component指的是bean
base-package 可以指定路径的包或者类解析
-->
<context:component-scan base-package="com.lzl.test.annotation.mvc"></context:component-scan>
</beans>
PersonAction类
@Controller 控制层的注解
@Resource 注入Bean
@Controller(value="personAction") //控制层的注解
public class PersonAction {
@Resource(name="personService") //注入的bean
private PersonService personService;
public PersonService getPersonService() {
return personService;
}
public void setPersonService(PersonService personService) {
this.personService = personService;
}
public void savePersonAction(){
this.personService.savePerson();
}
}
PersonServiceImpl类
@Service service层的注解
@Service(value="personService") //service层的注解
public class PersonServiceImpl implements PersonService{
@Resource(name="personDao")
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@Override
public void savePerson() {
this.personDao.savePerson();
}
}
PersonDaoImpl类
@Repository 持久层的注解
@Repository(value="personDao")
public class PersonDaoImpl implements PersonDao{
@Override
public void savePerson() {
System.out.println("保存用户");
}
}
注意
有时候我们的注解会出错,主要原因是没有分清楚,各层注解里面的bean的id是什么值。
所以出现注入Bean的错误时,要检查注入的value值是否一致。
spring容器中的继承
Java类中存在继承关系,那么通过xml文件如何表示类之间的继承关系呢。
xml文件提供了一下属性类表示继承关系。
parent=”idName” 表示子类拥有父类的属性和方法
abstract=”true” 定义抽象类
具体的例子如下
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-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!--
abstract
定义是否是抽象类
-->
<bean id="person" class="com.lzl.test.spring_extend.Person" abstract="true">
<property name="name" value="花虎沟"></property>
</bean>
<!--
parent
让子类拥有父类的属性和方法
-->
<bean id="student" class="com.lzl.test.spring_extend.Student" parent="person"></bean>
</beans>
Java类
Person.java
public abstract class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Student.java
public class Student extends Person{
public void say(){
System.out.println("hello:"+this.getName());
}
}
测试类
PersonTest.java
public class PersonTest extends SpringHelper {
static{
path="com/lzl/test/spring_extend/applicationContext.xml";
}
@Test
public void test(){
Student stu = (Student) fileResource.getBean("student");
stu.say();
}
}