用@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 ...
随机推荐
- LeetCode 二叉树的层次遍历 C++
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层 ...
- Kubernetes-Service Account
kube-apiserver 配置文件:/etc/kubernetes/apiserver KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0 ...
- 洛谷(P1006 传纸条)
题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个mm行nn列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运 ...
- linux挂载命令mount及U盘、移动硬盘的挂载
一.mount的命令格式是(注意mount只能在root权限下运行) mount dervice dir dervice是要挂载的设备,dir是挂载点 二.查看当前磁盘列表的设备 fdisk -l 显 ...
- 「日常训练」 Mike and Fun (CFR305D2B)
题意(CodeForces 548B) 每次对01矩阵中的一位取反,问每次操作后,单列中最长连续1的长度. 分析 非常非常简单,但是我当时训练的时候WA了四次...无力吐槽了,人间 不值得.jpg 代 ...
- Leetcode代码补全——链表
通过补全代码可以更深刻的体会到,链表就是一个存储方式,通过一单元的存储指向下一单元,而查看单元内容通过头部开始的指针依次遍历.这是leetcode里融合两个链表的题目,具体代码如下: #encodin ...
- [nginx] OpenResty 学习手册
OpenResty Installation Find tar.gz : https://openresty.org/cn/download.html tar -xzvf openresty-VERS ...
- 语法测试cnblogs使用Markdown
参考自作业部落Cmd Markdown 编辑器 https://www.zybuluo.com 欢迎使用 Cmd Markdown 编辑阅读器 什么是 Markdown Markdown 是一种方便记 ...
- UVA 1085 House of Cards(对抗搜索)
Description Axel and Birgit like to play a card game in which they build a house of cards, gaining ...
- java线程(3)——详解Callable、Future和FutureTask
回顾: 接上篇博客 java线程--三种创建线程的方式,这篇博客主要介绍第三种方式Callable和Future.比较继承Thread类和实现Runnable接口,接口更加灵活,使用更广泛.但这两种方 ...