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的更多相关文章

  1. Spring中的@Bean注解、@Configuration注解、@Value

    1.首先是注册bean类:@Component.@Responsitory.@Controller.@Service.Configuration这些注解是把要实例化的对象装化成一个bean,放到Ioc ...

  2. SpringBoot2.0之@Configuration注解

    SpringBoot2.0之@Configuration注解 本文转载自:https://www.javaman.cn/sb2/springboot-configuration 前面我们介绍了Spri ...

  3. Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解

    SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...

  4. Java开发学习(十)----基于注解开发定义bean 已完成

    一.环境准备 先来准备下环境: 创建一个Maven项目 pom.xml添加Spring的依赖 <dependencies>    <dependency>        < ...

  5. [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  6. 使用spring注解——定义bean和自动注入

    对于java bean的定义和依赖配置,使用xml文件真心是不方便. 今天学习如何用注解,解决bean的定义和注入. 常用注解: 1.自动注入:@Resources,@Autowired 2.Bean ...

  7. Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域

    1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...

  8. 多种方式实现依赖注入及使用注解定义bean

    构造注入 如何给构造方法中的参数注入方法呢如下 首先bean代码如下 package cn.pojo; public class Greeting { /** * 说的话 */ private Str ...

  9. SpringBoot学习之@Configuration注解和@Bean注解

    @Configuration 1.@Configuration注解底层是含有@Component ,所以@Configuration 具有和 @Component 的作用. 2.@Configurat ...

随机推荐

  1. (数据科学学习手札12)K-means聚类实战(基于R)

    上一篇我们详细介绍了普通的K-means聚类法在Python和R中各自的实现方法,本篇便以实际工作中遇到的数据集为例进行实战说明. 数据说明: 本次实战样本数据集来自浪潮集团提供的美团的商家信息,因涉 ...

  2. [BZOJ1076][SCOI2008]奖励关(概率DP)

    Code #include <cstdio> #include <algorithm> #include <cstring> #define N 110 #defi ...

  3. ABAP CDS ON HANA-(5)テーブル結合ビュー

    JOINs in CDS View In ABAP CDS, Join between two data sources is allowed. Allowed joins are:- Inner J ...

  4. 2018春季校园招聘笔经面经合集:Java开发岗

    2018春季校园招聘笔经面经合集:Java开发岗 以下为精选面经: 美团外卖配送部后台开发面经 nowcoder.com/discuss/76 春招总结,干货满满 nowcoder.com/discu ...

  5. win10 java环境变量配置

    首先,你应该已经安装了 Java 的 JDK 了(如果没有安装JDK,请跳转到此网址:http://www.oracle.com/technetwork/java/javase/downloads/i ...

  6. log报错: Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.

    报错: 解决方式: 1.登录数据库查看错误原因 结果发现账号无法正常登录出现账号被锁定的错误. 2.如何账号解锁? 用sys系统管理员账号登录数据库 SQL> alter user 用户名 ac ...

  7. ADVICE FOR SHORT-TERM MACHINE LEARNING RESEARCH PROJECTS(短期机器学习研究的建议)

    – Tim Rocktäschel, Jakob Foerster and Greg Farquhar, 29/08/2018 Every year we get contacted by stude ...

  8. python进阶训练

    1.列表,字典,集合解析 from random import randint #列表解析,选出大于0的元素 data=[randint(-10,10)for i in range(10)] resu ...

  9. UVA 11880 Ball in a Rectangle(数学+平面几何)

    Input: Standard Input Output: Standard Output � There is a rectangle on the cartesian plane, with bo ...

  10. Z.XML-Cocos2d-x开发笔记

    大家都在热火朝天的使用Cocos2d-x引擎做游戏开发,那么大家不妨把过程中解决的关键问题记录在这里,做一个分享! 1.在Android平台下打开网页 1.1修改项目工程源文件 在你的项目工程源文件中 ...