通过注解完成java对象创建,属性赋值
使用的步骤:
1. 加入maven依赖
2. 在类中加入Spring的注解
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 单元测试依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
</dependencies>
3. 在spring.xml的配置文件中,加入一个组件扫描的标签,说明注解在项目的位置
声明组件扫描器 (component-scan)
<context:component-scan base-package=""/>
组件就是Java对象
base-package :指定注解在项目中的包名
component-scan : spring会扫描遍历base-package指定的包,把包中和子包所有的类,找到类中的注解,按照注解的功能创建对象或给属性赋值
@Component:
创建对象的,等同于<bean>的功能
1. 属性:value 就是对象的名称,也就是<bean>的id值
@Component("xxx")或者@Component(value="xxx")
2. 位置:在类的上面
代码演示
student类
@Component("myStudent")
public class student {
@Value("李四")
private String name;
@Value("21")
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 +
'}';
}
}
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">
<context:component-scan base-package="cn.com.Ycy.spring.ba01"/>
</beans>
测试类
@Test
public void test01(){
String config="ApplicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
student student = (cn.com.Ycy.spring.ba01.student) ac.getBean("myStudent");
System.out.println(student);
}
赋值
1、简单类型的属性赋值
@value
1. 在属性定义的上面,无需set方法,推荐使用
2. 在set方法上面
示例代码:
@Value("李四")
public void setName(String name) {
this.name = name;
}
2、引用类型的属性赋值 @Autowired @Qualifier
1)(byType的自动注入) @Autowired 默认按类型自动装配Bean,可以加在属性上,也可以加在setter方法上
2)(byName 自动注入 )@Autowire + Qualifier("xxx") 按照属性名 匹配,如果没有匹配到就会报错,因为Autowire()默认是 required="true",如果设置为false,没有匹配到就只是把属性赋值为null,不会出错.
3)@Resource 默认是按照类名小写匹配,没有匹配到直接按照类型匹配,is a
关系,类似于autowire="byType".
4) @Resource(name="xxxx") 就是按照名字匹配,没匹配到就报错,类似autowire="byName".
衍生注解
为了更好的进行分层,Spring可以使用其他三个注解,功能和@Component一样
1、@Conteoller:web层,用在控制面板上面
2、@Service:service层,用在业务层类的上面
3、@Repository:dao层,用于持久层类的上面
以上三个注解的使用语法和Component一样,都能够创建对象,但是这三个注解还有额外的功能
@Repository、@Service、@Controler是给项目的对象分层的
总结一些小细节:
1、 Component的value是可以省略的,不写名称是默认:类名首字母小写,值要唯一
2、配置扫描多个包,可以使用逗号分开,分号也可以
第一种:
<context:component-scan base-package="cn.com.Ycy.spring.ba02"/>
<context:component-scan base-package="cn.com.Ycy.spring.ba03"/>
第二种:逗号分开,
<context:component-scan base-package="cn.com.Ycy.spring.ba02,cn.com.Ycy.spring.ba03"/>
第三种:分号分开;
<context:component-scan base-package="cn.com.Ycy.spring.ba02;cn.com.Ycy.spring.ba03"/>
3、XML与注解比较
1. XML可以适用任何场景 ,结构清晰,维护方便
2. 注解不是自己提供的类使用不了,开发简单方便
4、xml与注解整合开发 :推荐最佳实践
1. xml管理Bean
2. 注解完成属性注入