Spring学习二,基于注解的di

it2024-10-25  38

Spring

当项目中类相对少时,在一个配置文件中写多个bean对象,操作,修改,等相对简单些。但是项目大点,类的数量成百了等,若是同样写在同一个配置文件中,会使得文件内容繁多,修改操作麻烦,并且多人操作还可能带来冲突,矛盾,而换成多个配置文件每个配置文件的大小比一个文件的大小要小得多,效率也高些。

多配置文件的使用场合

按功能模块划分,一个模块一个配置文件;

按类的功能,数据库相关配置在一个配置文件中,做事务的功能在一个配置文件

基于注解的di

通过注解完成对象的创建,属性的赋值

使用注解的步骤

加入maven依赖spring-context,注意在加入spring-context依赖的同时,相当于间接加入了spring-aop的依赖。而加入了spring-aop的依赖,才可使用注解在类中加入spring的注解在spring的配置文件中,加入一个组建扫描器的标签,说明注解在项目中的位置

注解含义:

@Component 创建对象的,相当于配置文件中的bean标签的功能;属性有value,是对象的名称,也就是bean的id值,value值是唯一的,创建的对象在spring容器中就只有一个

创建对象的注解还有:

@Repository 用在持久层类上, 放在dao的实现类上,表示创建dao对象,dao对象是能访问数据库的;

@Service 用在业务层类上,放在service的实现类上,创建service对象,service对象是做业务处理的,可以由事务功能的

@Controller 用在控制器类上,创建控制类对象,控制器对象,能接受用户提交的参数,显示请求的处理结果

@Repository, @Service, @Controller这三个注解使用语法和@Component一样,又都能创建对象,但是这三个还有另外的功能,角色的声明,是给项目的对象进行分层的

当类不是持久层类,或业务层类,或控制层类时,还要创建对象的时候,这个时候是使用@Component的

使用位置:在类的上面

@Component(value = "myStudnet") public class Student { 相当于<bean id="myStudent" class="com.csdn.b01.Student"

原先配置文件的格式:

<!-- spring配置文件的标准格式--> <?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.xsd"> </beans>

如上添加了组件扫描器component-scan标签后,配置文件的改变:

<?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 https://www.springframework.org/schema/context/spring-context.xsd"> <?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 https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.csdn.b01"/> </beans> </beans>

读取配置文件时,从上往下依次读,发现组件扫描器,就去指定包下,找到类,看注解功能是去创建对象的,所以**spring容器就会调用无参构造方法创建注解所在类的对象,**对象名为value值,由此对象在容器中产生了

import org.springframework.stereotype.Component; //使用value属性指定对象名称 @Component(value = "myStudent") //或省略value //@Component("myStudent") //不指定对象名称,由spring提供默认名称,类名的首字母小写,即在测试获取bean对象时,代码修改为ac.getBean("student") public class Student { private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } @Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) ac.getBean("myStudent"); System.out.println(student); }
扫描多个包的方式
使用多次组件扫描器,指定不同包
<context:component-scan base-package="com.csdn.b01"/> <context:component-scan base-package="com.csdn.b02"/>
使用分隔符;或,分隔多个包名
<context:component-scan base-package="com.csdn.b01;com.csdn.b02"/>
指定父包
<context:component-scan base-package="com.csdn"/>

简单类型属性赋值

@Value 属性,value,是String类型的,表示简单类型的属性值

位置,在属性定义的上面,无需set方法

@Component(value = "myStudent") public class Student { @Value(value = "lisi") private String name; @Value(value = "29") private int age; @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

引用类型属性赋值

spring中通过注解给引用类型赋值,使用的是自动注入原理,支持byName,byType

@Autowired, spring框架提供的注解,实现引用类型的赋值,默认使用的是byType自动注入

推荐使用的位置:在属性定义的上面,无需set方法

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component(value = "myStudent") public class Student { @Value(value = "zhangsan") private String name; @Value(value = "33") private int age; @Autowired private School school; @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", school=" + school + '}'; } } package com.csdn.b01; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("mySchool") public class School { @Value(value = "北京大学") private String name; @Value(value = "北京海淀区") private String address; public void setName(String name) { this.name = name; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "School{" + "name='" + name + '\'' + ", address='" + address + '\'' + '}'; } } <?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 https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.csdn.b01"/> </beans> public class MyTest01 { @Test public void test(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) ac.getBean("myStudent"); System.out.println(student); } } /**Student{name='zhangsan', age=33, school=School{name='北京大学', address='北京海淀区'}}

若使用byName方式,需要在属性上面加入@Autowired;且在属性上面加入@Qualifier(value=“bean的id值”),表示使用指定名称的bean完成赋值

@Component(value = "myStudent") public class Student { @Value(value = "zhangsan") private String name; @Value(value = "33") private int age; @Autowired @Qualifier(value = "mySchool") private School school;

@Autowired注解,属性:required,是一个boolean类型的,默认true

required=true,表示若引用类型赋值失败,程序报错,终止执行;

required=false,表示引用类型若赋值失败,程序正常执行,引用类型是null

@Resource注解。来自于JDK的一个注解,是JDK的,不是spring的,只是spring提供了对jdk中@Resource的支持,和@Autowired一样用于给引用类型赋值。

使用的也是自动注入原理,支持byName,byType,默认是byName。先使用byName自动注入,如果byName赋值失败,再使用byType。如果只需要byName方式,需要增加一个属性name,name的值是bean的id名。

使用位置:在属性定义的上面,无需set方法,推荐使用

@Component(value = "myStudent") public class Student { @Value(value = "zhangsan") private String name; @Value(value = "33") private int age; //byType方式 //@Resource //byName方式 @Resource(name = "mySchool") private School school; @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", school=" + school + '}'; } } @Component("mySchool") public class School { @Value(value = "航空航天大学") private String name; @Value(value = "北京海淀区") private String address; @Override public String toString() { return "School{" + "name='" + name + '\'' + ", address='" + address + '\'' + '}'; } } /** Student{name='zhangsan', age=33, school=School{name='航空航天大学', address='北京海淀区'}}
最新回复(0)