用@Component注解代替@Configuration注解,定义bean
package com.timo.entity; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; /**
* 这里可以写@Component,@Service,@Controller注解,写@Repository注解会报错
* 报错的原因是在配置文件或者注解排除了@Repository.
*/
@Service
public class Dog {
// @Value("pug")
private String name;
// @Value("18")
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}
package com.timo.entity; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class Master {
@Autowired
private Dog dog;
private String name; public String getName() {
return name;
} public Master(String name) {
this.name = name;
} public void setName(String name) {
this.name = name;
} public Dog getDog() {
return dog;
} public void setDog(Dog dog) {
this.dog = dog;
}
public void showDogInfo(){
System.out.println("name="+dog.getName());
System.out.println("age="+dog.getAge());
}
}
package com.timo.entity; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component; @Component
public class FactoryMethodComponent {
@Bean
@Scope("prototype")
public static Dog dog(){
Dog dog = new Dog();
dog.setAge();
dog.setName("人民");
return dog;
}
@Bean
public static Master master(){
Master master = new Master("ouyangfeng");
master.setDog(dog()); return master;
}
public void doWork(){
System.out.println("I can do everything");
}
}
测试类的代码如下:
package com.timo.test2; import com.timo.entity.Dog;
import com.timo.entity.FactoryMethodComponent;
import com.timo.entity.Master;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test5 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(FactoryMethodComponent.class);
Dog dog = applicationContext.getBean(Dog.class);
System.out.println("name="+dog.getName());
System.out.println("age="+dog.getAge());
System.out.println("***********************");
FactoryMethodComponent factoryMethodComponent = applicationContext.getBean(FactoryMethodComponent.class);
factoryMethodComponent.doWork();
Master master = applicationContext.getBean(Master.class);
System.out.println("master "+master.getDog().getName());
}
}
用@Component注解代替@Configuration注解,定义bean的更多相关文章
- Spring中的@Bean注解、@Configuration注解、@Value
1.首先是注册bean类:@Component.@Responsitory.@Controller.@Service.Configuration这些注解是把要实例化的对象装化成一个bean,放到Ioc ...
- SpringBoot2.0之@Configuration注解
SpringBoot2.0之@Configuration注解 本文转载自:https://www.javaman.cn/sb2/springboot-configuration 前面我们介绍了Spri ...
- Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解
SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...
- Java开发学习(十)----基于注解开发定义bean 已完成
一.环境准备 先来准备下环境: 创建一个Maven项目 pom.xml添加Spring的依赖 <dependencies> <dependency> < ...
- [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...
- 使用spring注解——定义bean和自动注入
对于java bean的定义和依赖配置,使用xml文件真心是不方便. 今天学习如何用注解,解决bean的定义和注入. 常用注解: 1.自动注入:@Resources,@Autowired 2.Bean ...
- Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...
- 多种方式实现依赖注入及使用注解定义bean
构造注入 如何给构造方法中的参数注入方法呢如下 首先bean代码如下 package cn.pojo; public class Greeting { /** * 说的话 */ private Str ...
- SpringBoot学习之@Configuration注解和@Bean注解
@Configuration 1.@Configuration注解底层是含有@Component ,所以@Configuration 具有和 @Component 的作用. 2.@Configurat ...
随机推荐
- Python3爬虫(六) 解析库的使用之Beautiful Soup
Infi-chu: http://www.cnblogs.com/Infi-chu/ Beautiful Soup 借助网页的结构和属性等特性来解析网页,这样就可以省去复杂的正则表达式的编写. Bea ...
- 【Consul】关于健康检查的一点思考
健康检查是Consul提供的一项主要功能,其配置格式如下: { "check": { "id": "redis", "name&q ...
- Java笔试题-线程编程方面
Ja 线程编程方面 60.java中有几种方法可以实现一个线程?用什么关键字修饰同步方法?stop()和suspend()方法为何不推荐使用? 答:有两种实现方法,分别是继承Thread类与实现R ...
- 为什么mysqld启动报错
在一台ubuntu测试机器上启动一个mysql实例,本来应该是一件很简单的事情, 启动的时候却报错了: mysqld_safe --defaults-file=/etc/mysql/my3307. ...
- LeetCode 92 ——反转链表 II
1. 题目 2. 解答 我们需要先找到第 m 个结点及其上一个结点,然后将从 m 到 n 的结点进行反转,最后依次将 m 到 n 反转后的结点和 n 之后的结点放入原链表中即可. 从前往后依次遍历 m ...
- parity的使用
parity --chain dev --port 8045 ps aux | grep "parity" ps -elf | grep "pari"
- Pro Git - 笔记1
Getting Started About Version Control Local Version Control Systems Centralized Version Control Syst ...
- POJ 2082 Terrible Sets(栈)
Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...
- 【转】 The user specified as a definer ('root'@'') does not exist when using LOCK TALBE
在linux下,用mysql的导出语句: mysqldump -u root -pPasswd table >/home/lsf/test.sql 出现了 Got error: 1449: Th ...
- lintcode-115-不同的路径 II
115-不同的路径 II "不同的路径" 的跟进问题: 现在考虑网格中有障碍物,那样将会有多少条不同的路径? 网格中的障碍和空位置分别用 1 和 0 来表示. 注意事项 m 和 n ...