1 使用注解
在spring4之后,想要使用注解形式,必须得要引入aop的包
org.springframework:spring-aop:5.2.0.RELEASE
在配置文件当中,还得要引入一个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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
Bean的实现
我们之前都是使用 bean 的标签进行bean注入,但是实际开发中,我们一般都会使用注解!
beans.xml
1 配置扫描哪些包下的注解
<!-- 开启注解支持-->
<context:annotation-config/>
<!--指定扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.zs.pojo"/>
2 在指定包下编写类,增加注解
User.class
package com.zs.pojo;
import org.springframework.stereotype.Component;
@Component //组件
public class User {
public String name = "zs";
}
3 属性如何注入
package com.zs.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component //组件
public class User {
@Value("zs") // 简单属性注入
public String name;
}
4 衍生注解
@Component三个衍生注解
为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。
@Controller:web层
@Service:service层
@Repository:dao层
写上这些注解,就相当于将这个类交给Spring管理装配了!
5 作用域
@scope
singleton:默认的,Spring会采用单例模式创建这个对象。关闭工厂 ,所有的对象都会销毁。
prototype:多例模式。关闭工厂 ,所有的对象不会销毁。内部的垃圾回收机制会回收
@Component //组件
@Scope("singleton")
public class User {
@Value("zs") // 简单属性注入
public String name;
}
6 小结
XML与注解比较
1 XML可以适用任何场景 ,结构清晰,维护方便
2 注解不是自己提供的类使用不了,开发简单方便
xml与注解整合开发 :推荐最佳实践
1 xml管理Bean
2 注解完成属性注入
开启注解支持
<context:annotation-config/>
2 使用java的方式配置spring
完全不适用spring的xml配置,全部交给java来做
1 编写一个实体类
package com.zs.pojo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
@Data
@Repository //这个注解的意思,就是说明这个类被spring接管了,注册到容器中
public class User {
@Value("zs") //属性注入值
public String name;
}
2 新建一个config配置包
package com.zs.config;
import com.zs.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration // 这个也会被spring容器托管,注册到容器中 因为它本身是一个@Component
// @Configuration代表这是一个配置类 就和我们之前看的beans.xml
@Import(UserConfig1.class) // 导入另一个配置类UserConfig1
public class UserConfig {
// 注册一个bean 就相当于我们之前写的一个bean标签
// 这个方法的名字就相当于bean标签中的id属性
// 这个方法的返回值 就相当于bean标签中的class
@Bean
public User getUser() {
return new User(); //就是返回要注入到bean的对象
}
}
3 测试类
package com.zs.test;
import com.zs.config.UserConfig;
import com.zs.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test() {
// AnnotationConfigApplicationContext 来获取容器
ApplicationContext context = new AnnotationConfigApplicationContext(UserConfig.class);
User user = (User) context.getBean("getUser");
System.out.println(user.name);
}
}
另外