作者:有只小猪飞走了
博客地址:https://blog.csdn.net/m0_62262008?type=blog
这期内容:揭开Bean存储的神秘面纱
本文章是由博主自己总结,如果哪里有错误或者不全的,希望各位大佬指出,又或者你有什么不懂的,都可以私聊我哈~谢谢啦!
Spring中存储Bean的注解有两种:
1,类注解:@Controller、@Service、@Repository、@Component、@Configuration
2,方法注解:@Bean
这里给你们一一介绍每种注解以及其用法-----》
其实@Controller注解就是一个控制存储的类注解,代码如下:
package com.tom.controller;
import org.springframework.stereotype.Controller;
@Controller
public class UserController4 {
public void sayHello(){
System.out.println("Do User Controller");
}
}
我们来读取一下UserCoontroller4的方法:
import com.tom.controller.UserController4;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
UserController4 userController4 =
context.getBean("userController4",UserController4.class);
userController4.sayHello();
}
}
运行main方法,我们可以得到以下结果:
我们可以看到,在类的上面加上@Controller即可。同理其他也一样,下面我们一一举例。
我们可以通过以下代码来检验:
package com.tom.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void doService(){
System.out.println("Do user service");
}
}
读取一下UserService:
import com.tom.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService =
context.getBean("userService",UserService.class);
userService.doService();
}
}
我们可以得到以下结果:
我们可以通过以下代码来检验:
package com.tom.repository;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository {
public void doRepository(){
System.out.println("Do User Repository");
}
}
我们来读取一下UserRepository方法:
import com.tom.repository.UserRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
UserRepository userRepository =
context.getBean("userRepository",UserRepository.class);
userRepository.doRepository();
}
}
我们运行main方法,可以得到以下结果:
我们可以通过以下代码来检验:
package com.tom.component;
import org.springframework.stereotype.Component;
@Component
public class UserComponent {
public void doComponent(){
System.out.println("Do UserComponent");
}
}
读取bean:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
UserComponent userComponent =
context.getBean("userComponent",UserComponent.class);
userComponent.doComponent();
}
}
运行main方法,可得到以下结果:
我们可以通过以下代码来检验:
package com.tom.configuration;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfiguration {
public void doConfig(){
System.out.println("Do UserConfiguration");
}
}
读取Bean:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
UserConfiguration userConfiguration =
context.getBean("userConfiguration",UserConfiguration.class);
userConfiguration.doConfig();
}
}
运行main方法可以得到以下结果:
(1)类注解是添加到某个类上的,⽽⽅法注解是放到某个⽅法上的,如以下代码的实现:
package com.tom.controller;
import com.tom.model.User;
import org.springframework.context.annotation.Bean;
public class UserBean2 {
@Bean
public User user1(){
User user = new User();
user.setId(1);
user.setName("UserBean:lisi");
user.setAge(18);
return user;
}
}
我们读取Bean:
import com.tom.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
User user = (User) context.getBean("user1");
System.out.println(user.toString());
}
}
我们可以看到如下的运行结果:
(2)从上面的运行错误可以知道:方法注解一定要配合类注解一起使用!
在 Spring 框架的设计中,⽅法注解 @Bean 要配合类注解才能将对象正常的存储到 Spring 容器中,如下代码所示:
package com.tom.controller;
import com.tom.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class UserBean2 {
@Bean
public User user1(){
User user = new User();
user.setId(1);
user.setName("UserBean:lisi");
user.setAge(18);
return user;
}
}
再次运行main函数,可得以下结果:
(3)重命名@Bean
可以通过设置 name 属性给 Bean 对象进⾏重命名操作,如下代码所示:
package com.tom.controller;
import com.tom.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class UserBean2 {
@Bean(name = "tom1")
public User user1(){
User user = new User();
user.setId(1);
user.setName("UserBean:lisi");
user.setAge(18);
return user;
}
}
读取Bean可以这样写:
import com.tom.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
User user = (User) context.getBean("tom1");
System.out.println(user);
}
}
运行main可以得到一样的结果:
这个重命名的 name 其实是⼀个数组,⼀个 bean 可以有多个名字:
package com.tom.controller;
import com.tom.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class UserBean2 {
@Bean(name = {"tom1", "tom2"})
public User user1(){
User user = new User();
user.setId(1);
user.setName("UserBean:lisi");
user.setAge(18);
return user;
}
}
可以这样子读取Bean:
import com.tom.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
User user = (User) context.getBean("tom2");
System.out.println(user);
}
}
同样可以得到这样的结果:
并且 name={} 可以省略,如下代码所示:
package com.tom.controller;
import com.tom.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class UserBean2 {
@Bean( {"tom1", "tom2"})
public User user1(){
User user = new User();
user.setId(1);
user.setName("UserBean:lisi");
user.setAge(18);
return user;
}
}
同理,如果有多个构造方法,我们可以通过重命名的方式来确定打印哪个,如下:
package com.tom.controller;
import com.tom.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class UserBeans {
//Bean注解一定要配合五大类注解一起使用
@Bean(name = "user_user1")
public User user1() {
User user = new User();
user.setId(1);
user.setName("UserBean:lisi");
user.setAge(18);
return user;
}
@Bean
public User user2() {
User user = new User();
user.setId(2);
user.setName("UserBean:lisi");
user.setAge(18);
return user;
}
}
读取其中user2的Bean:
import com.tom.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
User user = (User) context.getBean("tom3");
System.out.println(user);
}
}
运行main,可以得到以下结果:
Bean注解只可以使用在无参的方法上(因为Spring无法提供参数),如下代码:
package com.tom.controller;
import com.tom.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class UserBean2 {
@Bean( {"tom1", "tom2"})
public User user1(){
User user = new User();
user.setId(1);
user.setName("UserBean:lisi");
user.setAge(18);
return user;
}
@Bean(name = "tom3")
public User user2(Integer id){
User user = new User();
user.setId(1);
user.setName("UserBean:lisi2");
user.setAge(18);
return user;
}
}
读取其中的user2的Bean:
import com.tom.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App3 {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml");
User user = (User) context.getBean("tom3");
System.out.println(user);
}
}
读到的结果会出现错误提示:
以上就是这期博客的所有内容,希望对你有帮助!
记得关注小猪~
下期再见!