写在前面:
在spring4之后,想要使用注解形式,必须得要引入aop的包。
在配置文件中,引入一个context约束
<?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
https://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.damin.pojo"/>
<!-- 导入context约束,增加注解支持-->
<context:annotation-config/>
</beans>
Bean的实现
我们之前都是使用bean的标签进行bean注入,但是实际开发中,我们一般都会使用注解。
1.配置扫面哪些包下的注解
<!-- 指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.damin.pojo"/>
2.在指定包下编写类,增加注解
package com.damin.pojo;
import org.springframework.stereotype.Component;
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@Component
public class User {
public String name = "wudimin";
}
3.测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user.name);
}
属性注入
使用注解注入属性
1.可以不用提供set方法,直接在属性名上添加@value("值")
package com.damin.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
@Component
public class User {
@Value("widimin")
public String;
}
2.如果提供了set方法,在set方法上添加@value("值")
package com.damin.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User {
public String name;
@Value("widimin")
public void setName(String name) {
this.name = name;
}
}
@Component三个衍生注解
衍生注解,就是替代了在配置文件当中配置步骤,更加的方便快捷。
为了更好的进行分层,Spring可以使用其它三个注解,功能一样。
- @Controller:web层
- Service:service层
- Repository:dao层
写上这些注解,就相当于将这个类交给Spring管理装配了。
XML与注解比较
- XML可以使用任何场景,结果清晰,维护方便
- 注解不是自己提供的类使用不了,开发简单方便
基于Java类进行配置
JavaConfig 原来是Spring的一个子项目,它通过Java类的方式提供Bean的定义信息,在Spring4的版本,JavaConfig已正式成为Spring4的核心功能。
1.编写一个实体类
package com.damin.pojo;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
2.新建一个config配置包,编写一个MyConfig配置类
package com.damin.config;
import com.damin.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
* @Configuration代表这是一个配置类 --> 之前写的beans.xml
*/
@Configuration
public class MyConfig {
/*
* 注册一个@Bean,就相当于之前写的一个bean标签
* 这个方法的名字,就相当于bean标签中的id
* 返回值,就相当于bean标签中的class属性。
*/
@Bean
public User getUser(){
return new User();
}
}
3.测试
import com.damin.config.MyConfig;
import com.damin.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
public static void main(String[] args) {
//AnnotationConfigApplicationContext(MyConfig.class);
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = (User) context.getBean("getUser");
System.out.println(getUser.getName());
}
}
需要导入其它配置时的操作
1.编写一个配置类
@Configuration //代表这是一个配置类
public class MyConfig2 {
}
2.在之前的配置类中选择导入这个配置类
@Configuration
@Import(MyConfig2.class) //导入合并其他配置类,类似于配置文件中的 inculde 标签
public class MyConfig {
@Bean
public User getUser(){
return new User();
}
}