spring注解第01课 @Configuration、@Bean
一、原始的 xml配置方式
1.Spring pom 依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
2.JavaBean
public class Person {
private String name;
private Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = 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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
3.beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="person" class="com.atguigu.bean.Person">
<property name="name" value="张三" />
<property name="age" value="10" />
</bean> </beans>
4.测试类
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.atguigu.bean.Person; public class Test { public static void main(String[] args) {
ClassPathXmlApplicationContext beans = new ClassPathXmlApplicationContext("beans.xml");
Person p = (Person)beans.getBean("person");
System.out.println(p); } }
二、注解的形式
1. @Configuration替代beans.xml,@Bean 替代<bean>
package com.atguigu.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans; import com.atguigu.bean.Person; //配置类==配置文件
@Configuration //告诉Spring这是一个配置类
public class MainConfig {
//给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
@Bean("person")
public Person person01(){
return new Person("lisi", 20);
} }
2.测试用例
package com.atguigu; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.atguigu.bean.Person;
import com.atguigu.config.MainConfig; public class MainTest { @SuppressWarnings("resource")
public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean); String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
for (String name : namesForType) {
System.out.println(name);
}
}
}
spring注解第01课 @Configuration、@Bean的更多相关文章
- spring注解第05课 FactoryBean
1.工厂bean调用 @Configuration public class MainConfig2 {/** * 使用Spring提供的 FactoryBean(工厂Bean); * 1).默认获取 ...
- spring注解第03课 按条件加载Bean @Conditional
package com.atguigu.config; import org.springframework.context.annotation.Bean; import org.springfra ...
- spring注解第02课 包扫描@ComponentScan、@ComponentScans
1.配置文件形式: <context:component-scan base-package="com.atguigu" /> spring会扫描此包下的@Servic ...
- spring注解第04课 @Import
1.beans package com.atguigu.bean; public class Blue { public Blue(){ System.out.println("blue.. ...
- spring注解第07课 @Valid和@Validated的总结区分
@Valid: @Valid注解用于校验,所属包为:javax.validation.Valid. ① 首先需要在实体类的相应字段上添加用于充当校验条件的注解,如:@Min,如下代码(age属于Gir ...
- spring注解第06课 @Value
1.注入<bean>中的属性 支持3种类型的赋值 <bean id="person" class="com.model.Person"> ...
- Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解
基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置S ...
- [转]Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...
- Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...
随机推荐
- [CF977F]Consecutive Subsequence
题目描述 You are given an integer array of length n. You have to choose some subsequence of this array o ...
- [HAOI2012]道路(最短路DAG上计数)
C国有n座城市,城市之间通过m条[b]单向[/b]道路连接.一条路径被称为最短路,当且仅当不存在从它的起点到终点的另外一条路径总长度比它小.两条最短路不同,当且仅当它们包含的道路序列不同.我们需要对每 ...
- springAop @AfterReturning注解 获取返回值
@AfterReturning(returning="rvt", pointcut="@annotation(com.sinosoft.redis.cache.PutCa ...
- js 获取对象属性个数
js 获取对象属性个数 方法一: var attributeCount = function(obj) { var count = 0; for(var i in obj) { if(obj.hasO ...
- A1057. Stack
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- 【译】3. Java反射——构造函数
原文地址:http://tutorials.jenkov.com/java-reflection/constructors.html ================================= ...
- 【译】7. Java反射——私有字段和私有方法
原文地址:http://tutorials.jenkov.com/java-reflection/private-fields-and-methods.html =================== ...
- 第十六节,使用函数封装库tf.contrib.layers
这一节,介绍TensorFlow中的一个封装好的高级库,里面有前面讲过的很多函数的高级封装,使用这个高级库来开发程序将会提高效率. 我们改写第十三节的程序,卷积函数我们使用tf.contrib.lay ...
- spring的设计模式
spring中用到哪些设计模式 1.工厂模式,这个很明显,在各种BeanFactory以及ApplicationContext创建中都用到了: 2.模版模式,这个也很明显,在各种BeanFacto ...
- emacs简易配置
(setq-default inhibit-startup-message t tab-width 4 c-basic-offset 4 indent-tabs-mode t) (tool-bar-m ...