把在Spring的IOC容器中存放的对象,叫做bean.本文着重介绍bean的存储相关的注解及其用法.
1. 三种获取bean的方法
这里只介绍三种获取bean的方法:
Object getBean(String var1)
Object getBean(Class<T> var1)
Object getBean(String var1,Class<T> var2)
分别是根据bean的名称获取,根据bean的类型获取,根据bean的名称和类型获取.我们获取到的bean对象都是同一个,属于单例.
bean的命名:
bean名称以小写字母开头,然后使用驼峰式大小写.
比如: UserController这个类的bean的名称是userController.
也有特殊情况:当有多个字符并且第一个和第⼆个字符都是大写时,将保留原始的大小写.
比如: UController这个类的bean名称是UController.
2. 类注解
2.1 @Controller(控制器存储)
@Controller
public class UserController {
public void sayHi() {
System.out.println("hi,controller");
}
}
获取到UserController对象,调用sayHi方法:
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
UserController userController = context.getBean(UserController.class);
userController.sayHi();
}
这就是通过类名的方式来获取bean.
2.2 @Service(服务存储)
@Service
public class UserService {
public void sayHi() {
System.out.println("hi,service");
}
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
UserService userService = context.getBean(UserService.class);
userService.sayHi();
}
2.3 @Repository(仓库存储)
@Repository
public class UserRepository {
public void sayHi() {
System.out.println("hi,repository");
}
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
UserRepository userRepository = context.getBean(UserRepository.class);
userRepository .sayHi();
}
2.4 @Component(组件存储)
@Component
public class UserComponent {
public void sayHi() {
System.out.println("hi,component");
}
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
UserComponent userComponent = context.getBean(UserComponent.class);
userComponent.sayHi();
}
2.5 @Configuration(配置存储)
@Configuration
public class UserConfiguration {
public void sayHi() {
System.out.println("hi,configuration");
}
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
UserConfiguration userConfiguration = context.getBean(UserConfiguration.class);
userConfiguration.sayHi();
}
3. 方法注解@Bean
只使用类注解,有些问题就无法解决:外部包里面的类无法添加类注解,一个类可能有多个数据源需要多个对象.这时,我们仅仅使用类注解无法解决问题,就引入了方法注解@Bean.
使用@Bean注解时,需要搭配上述的类注解一同使用.
3.1 获取单个bean
@Component
public class BeanConfig {
@Bean
public User user(){
User user = new User();
user.setName("zhangsan");
user.setAge(22);
return user;
}
}
@Data
public class User {
public String name;
public int age;
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
User user = context.getBean(User.class);
System.out.println(user);
}
3.2 获取多个bean
这时必须使用名称来获取bean:
@Component
public class BeanConfig {
@Bean
public User user1(){
User user = new User();
user.setName("zhangsan");
user.setAge(22);
return user;
}
@Bean
public User user2(){
User user = new User();
user.setName("lisi");
user.setAge(19);
return user;
}
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
User user1 = (User)context.getBean("user1");
User user2 = (User)context.getBean("user2");
System.out.println(user1);
System.out.println(user2);
}
3.3 bean的重命名
@Component
public class BeanConfig {
@Bean(name = {"u1","u2"})
public User user1(){
User user = new User();
user.setName("zhangsan");
user.setAge(22);
return user;
}
}
"name="这部分可以省略,如果只有一个重新命名的名字,{}也可以省略.
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
User user1 = (User)context.getBean("u1");
System.out.println(user1);
}
3.4 扫描路径
我们之前加上了@Bean注解不一定会生效,如果想要生效,需要Spring扫描到.而Spring扫描的默认路径是Application这个类所在的包中.我如果将它放入controller包中,Spring就扫描不到component这个包下面的bean了.我们可以通过@ComponentScan来指定扫描路径.
@ComponentScan("{com.lzq.demo}")
@SpringBootApplication
public class SpringIocApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
User user1 = (User)context.getBean("u1");
System.out.println(user1);
}
}