将对象存储在 Spring 中,有两种注解类型可以实现:
如下使用@Controller存储Bean代码:
@Controller
public class UserController {
public void SayHi(String name){
System.out.println("你好"+name);
}
}
使用获取上下文的方法获取上面存储的对象
public class Test {
public static void main(String[] args) {
//获取spring上下文
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
//获取指定的Bean对象名称+类型进行获取
UserController userController = (UserController) applicationContext.getBean("userController");
userController.SayHi("zcx");
}
}
如下使用@Service存储Bean代码:
@Service
public class UserService {
public void SayHi(String name){
System.out.println("你好"+name);
}
}
输出结果跟@Controller一样
如下使用@Repository存储Bean代码:
@Repository
public class UserRepository {
public void SayHi(String name){
System.out.println("你好"+name);
}
}
如下使用@Component存储Bean代码:
@Component
public class UserComponent {
public void SayHi(String name){
System.out.println("你好"+name);
}
}
如下使用@Configuration存储Bean代码:
@Configuration
public class UserConfiguration {
public void SayHi(String name){
System.out.println("你好"+name);
}
}
需要这么多类注解是让程序员看到类注解之后,就能直接了解当前类
的⽤途,比如:
⽅法注解是放到某个⽅法上的要配合类注解使⽤,如以下代码的实现:
@Controller
public class Users {
@Bean
public User user1(){
User user = new User();
return user;
}
}
public class Test {
public static void main(String[] args) {
//获取spring上下文
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
//获取指定的Bean对象名称+类型进行获取
User user = (User) applicationContext.getBean("user1");
user.sayHi("zcx");
}
}
user类
@Component
public class User {
public void sayHi(String name){
System.out.println(name+"你好");
}
}
可通过设置 name 属性给 Bean 对象进⾏重命名操作
@Bean(name = {"u1", "us1"})
public User user1() {
User user = new User();
return user;
}
或者
@Bean(name = {"u1", "us1"})
public User user1() {
User user = new User();
return user;
}
获取Bean对象就是把对象取出来放入某个类中,也叫对象注入或者对象装配。
对象注入有三种实现方法:
下⾯我们按照实际开发中的模式,将 Service 类注⼊到 Controller 类中
属性注⼊是使⽤ @Autowired 实现的,实现代码如下:
@Service
public class UserService {
public User user1(){
User user = new User();
return user;
}
}
@Controller
public class UserController {
@Autowired
private UserService userService;
public User getUser(){
return userService.user1();
}
}
public class Test {
public static void main(String[] args) {
//获取spring上下文
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
//获取指定的Bean对象名称+类型进行获取
UserController userController = applicationContext.getBean(UserController.class);
userController.getUser().sayHi("zcx");
}
}
构造⽅法注⼊是在类的构造⽅法中实现注⼊,实现代码如下:
@Controller
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
public User getUser(){
return userService.user1();
}
}
其余代码写法跟属性注入一样
在设置 set ⽅法的时候需要加上 @Autowired 注解
@Controller
public class UserController {
private UserService userService;
@Autowired
public void SetUserController(UserService userService) {
this.userService = userService;
}
public User getUser(){
return userService.user1();
}
}
@Controller
public class UserController {
@Resource
private UserService userService;
public User getUser(){
return userService.user1();
}
}
使用@Resource 可以解决同⼀类型多个 Bean 报错问题
可以如下设置方法名
@Resource(name = "user1")
@Autowired 和 @Resource 的区别