目录
- 1.1 配置 spring.xml 设置 spring 存入对象的根路径
- 1.2 使用注解将 Bean 对象存储到 spring 中
- 1.3 类注解
- 1.4 类注解之间的关系
- 1.5 spring 中五大类生成 beanname 的问题
- 1.6 方法注解
1、Spring 更简单的存储对象
使用注解来进行 spring 更为简单得存储和读取对象得操作
1.1 配置 spring.xml 设置 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"
xmlns:content="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">
<content:component-scan base-package=""></content:component-scan>
</beans>
1.2 使用注解将 Bean 对象存储到 spring 中
想要将 Bean 对象存储在 Spring 中, 有两种注解类型可以实现:
- 1、 类注解 :
@Controller @Service @Repository @Component @Configuration
- 2、方法注解 :
@Bean
1.3 类注解
① @Controller 控制器【对象的存储】
@Controller
public class UserController {
public void sayHi(){
System.out.println("你好,UserController");
}
}
② @Service 【服务存储】
@Service
public class UserService {
public void sayHi(){
System.out.println("你好,UserService");
}
}
③ @Repository 【仓库存储】
@Repository
public class UserRepository {
public void sayHi(){
System.out.println("你好,UserRepository");
}
}
④ @Component 【组件存储】
@Component
public class UserComponent {
public void sayHi(){
System.out.println("你好,Component");
}
}
⑤ @Configuration 【配置存储】
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
public void sayHi(){
System.out.println("你好,Config");
}
}
测试读取 bean 查看是否通过注解把 bean 注册到 spring 内
import com.beans.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-04
* Time: 16:28
*/
public class APP {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserController userController = context.getBean("userController", UserController.class);
userController.sayHi();
ApplicationContext context1 = new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService = context1.getBean("userService",UserService.class);
userService.sayHi();
ApplicationContext context2 = new ClassPathXmlApplicationContext("spring-config.xml");
UserRepository userRepository = context2.getBean("userRepository",UserRepository.class);
userRepository.sayHi();
ApplicationContext context3 = new ClassPathXmlApplicationContext("spring-config.xml");
UserConfig userConfig = context3.getBean("userConfig",UserConfig.class);
userConfig.sayHi();
ApplicationContext context4 = new ClassPathXmlApplicationContext("spring-config.xml");
UserComponent userComponent = context4.getBean("userComponent",UserComponent.class);
userComponent.sayHi();
}
}
注意事项:
为什么需要五大类注解:
每一个注解对应的
业务场景是不同的
,为了让程序员看到注解,就能了解当前类的用途,提高程序可读性
1.4 类注解之间的关系
@Controller / @Service / @Repository / @Configuration 都是基 Component 实现的,所以 @Component 是其他 4个 注解的父类
1.5 spring 中五大类生成 beanname 的问题
在 Idea 中使用搜索关键字 beanName
1.6 方法注解
@Bean注解(需要配合五大类注解进行使用)
①、先创建一个类
package com.beans;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-05
* Time: 10:29
*/
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
②、创建实例(需要进行存储的对象):方法返回的对象就是存储到 spring 中的对象
package com.beans;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-05
* Time: 10:30
*/
@Controller
public class UserBean {
@Bean
public User user1(){
User user = new User();
user.setAge(10);
user.setName("张三");
return user;
}
}
③、测试看是否方法注释使 bean 对象注册成功:
ApplicationContext context5 = new ClassPathXmlApplicationContext("spring-config.xml");
User user = context5.getBean("user1",User.class);
System.out.println("name" + user.getName() + ",age :"+user.getAge());
方法注解 重命名 Bean
上面获取到 user 对象是使用
方法名来取的
,这里进行重命名后就可以通过重命名的名字来进行访问
package com.beans;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-08
* Time: 12:02
*/
@Controller
public class UserBean {
@Bean(name = "user")
public User user1(){
User user = new User();
user.setName("张三");
user.setId(1);
return user;
}
}
也可以重命名
多个
名字:
@Bean(name = {"user","zhangsan"})
注意事项:
Bean 命名规则:当没有重命名的时候,默认为
方法名
,当重命名后,只能通过重命名的名字进行获取
2. Spring 更简单的获取对象
注解一、Autowired
2.1 属性注入
package com.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-08
* Time: 12:51
*/
@Controller
public class UserController {
@Autowired
private UserServer userServer;
public void sayHi(){
userServer.sayHi();
}
}
package com;
import com.beans.User;
import com.beans.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-08
* Time: 12:06
*/
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
UserController userController = context.getBean(UserController.class);
userController.sayHi();
}
}
2.2 通过构造方法进行对象注入(官方推荐写法)
package com.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-08
* Time: 13:44
*/
@Controller
public class UserController2 {
private UserServer userServer;
@Autowired
public UserController2(UserServer userServer){
this.userServer = userServer;
}
public void sayHi(){
userServer.sayHi();
}
}
2.3 通过 Setter 进行对象注入
package com.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-08
* Time: 13:44
*/
@Controller
public class UserController2 {
private UserServer userServer;
@Autowired
public void setUserServer(UserServer userServer) {
this.userServer = userServer;
}
public void sayHi(){
userServer.sayHi();
}
}
注解二、Resource(不适用于与构造方法,另外两个都可以)
package com.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
/**
* Created with IntelliJ IDEA.
* Description:
* User: Lenovo
* Date: 2022-07-08
* Time: 13:44
*/
@Controller
public class UserController2 {
private UserServer userServer;
@Resource
public void setUserServer(UserServer userServer) {
this.userServer = userServer;
}
public void sayHi(){
userServer.sayHi();
}
}
对于构造方法不支持:
2.4、属性注入,构造方法注入,set 注入区别是什么
①、属性注入:写法简单,但是通用性不好,只能运行在 IoC 容器下,如果是非 IoC 容器就会出现问题
②、Setter 注入:早期 String 推荐的方法,Setter 注入通用性没有 构造方法注入 通用
③、构造方法注入:通用性更好(当今推荐的),可以确保在使用对象注入之前,此注入对象一定初始化过了,当构造方法参数注入过多时,此时开发者就需要检查自己写的代码是否符合单一设计原则的规范了,此注入方式也是 spring 后期版本中,推荐的注入方式
2.5 @Autowired VS @Resource 的区别
1、出身不同:@Resource 来自于
jdk
,@Autowired 是spring
框架提供的
2、用法不同:Autowired 支持属性注入,构造方法注入,Setter 注入
,Resource不支持构造方法注入
3、支持的参数不同:@Resource 支持参数的设置,比如name,type ……属性设置
而 @Autowired 只支持required 参数设置
2.6 同一类型多个@Bean报错
解决办法1: 使用 @Resource(name=‘别名’):
@Controller
public class UserController3 {
@Resource(name = "user2")
private User user;
public void getUserInfo(){
System.out.println(user.getName() + " " + user.getId());
}
}
解决办法2: 使用 @Qualifier 注解定义名称
@Controller
public class UserController3 {
@Autowired
@Qualifier(value = "user1")
private User user;
public void getUserInfo(){
System.out.println(user.getName() + " " + user.getId());
}
}