spring注解开发:Configuration&Bean
1、使用xml创建bean的方式
1、首先新建一个maven工程,添加如下依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2、其次新建一个Person对象
package com.yefengyu.annotation.bean; 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、编写spring配置文件
<bean id="person" class="com.yefengyu.annotation.bean.Person">
<property name="name" value="yefengyu"></property>
<property name="age" value="28"></property>
</bean>
4、测试
public static void main(String[] args)
{
ApplicationContext ctx= new ClassPathXmlApplicationContext("spring.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
2、使用注解创建bean的方式
1、不需要xml文件,但是需要一个可以替换xml配置文件的配置类,也就是上面第三步可以被删除,使用如下代码:
package com.yefengyu.annotation.config; import com.yefengyu.annotation.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //配置类等于配置文件
@Configuration//告诉spring这是一个配置类
public class MainConfig
{
@Bean//给容器注册一个bean,类型为返回值类型,id默认为方法名称
public Person person()
{
return new Person("yefengyu", 28);
}
}
2、测试
public static void main(String[] args)
{
ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class);
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
3、注意点:
(1)ApplicationContext 使用AnnotationConfigApplicationContext来获取,并且参数为配置类而非配置文件。
(2)在MainConfig配置类中,使用Bean注解给容器注册了一个bean,bean的id为方法名称person,因此可以在测试main方法中使用 person 作为bean的id获取bean。
(3)除了使用bean的id来从容器中获取bean,还可以使用bean的类型来获取,因此下面的这句
Person person = (Person) ctx.getBean("person");
可以改为如下代码,无需强转:
Person person = ctx.getBean(Person.class);
(4)如何给bean起个名字?在上面的MainConfig类中,id默认为方法名称,如何另起一个名称呢?只需要在bean注解上面加一个值,表示使用我们指定的注解,而不使用默认的注解。
@Bean("person")
public Person getPerson()
{
return new Person("yefengyu", 28);
}
上面的代码中,我们可以指定了bean的id为 person,而非默认的 getPerson。注意只要指定了bean的id,就不能使用默认的id。
(5)查看注册的bean的名称(ID)
public static void main(String[] args)
{
ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class);
String[] names = ctx.getBeanDefinitionNames();
for (String name : names)
{
System.out.println(name);
}
}
结果如下:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
person
这里我们看到除了spring框架自身的bean之外,还有mainConfig,也就是注解配置实例,此外就是我们关注的person这个bean了。
spring注解开发:Configuration&Bean的更多相关文章
- spring注解开发:bean的作用域与懒加载
1.bean的作用域 1.新建一个maven工程,添加如下依赖 <dependency> <groupId>org.springframework</groupId> ...
- spring注解开发:容器中注册组件方式
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...
- Spring注解开发系列Ⅵ --- AOP&事务
注解开发 --- AOP AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取.详细的AO ...
- 浅尝Spring注解开发_自定义注册组件、属性赋值、自动装配
Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含自定义扫描组件.自定义导入组件.手动注册组件.自动注入方法和参数.使用Spring容器底层组件等 配置 @Confi ...
- 浅尝Spring注解开发_Bean生命周期及执行过程
Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含Bean生命周期.自定义初始化方法.Debug BeanPostProcessor执行过程及在Spring底层中的应 ...
- 浅尝Spring注解开发_AOP原理及完整过程分析(源码)
浅尝Spring注解开发_AOP原理及完整过程分析(源码) 浅尝Spring注解开发,基于Spring 4.3.12 分析AOP执行过程及源码,包含AOP注解使用.AOP原理.分析Annotation ...
- Spring注解开发_Spring容器创建概述
浅尝Spring注解开发_Spring容器创建概述 浅尝Spring注解开发,基于Spring 4.3.12 概述Spring容器创建的过程,包括12个方法的执行 浅尝Spring注解开发_自定义注册 ...
- Java开发学习(十)----基于注解开发定义bean 已完成
一.环境准备 先来准备下环境: 创建一个Maven项目 pom.xml添加Spring的依赖 <dependencies> <dependency> < ...
- Annotation(三)——Spring注解开发
Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
随机推荐
- 如何将网络流转化为内存流 C#
//将获取的文件流转化为内存流 public static MemoryStream ConvertStreamToMemoryStream(Stream stream) { MemoryStream ...
- elasticsearch 深入 —— Search After实时滚动查询
Search After 一般的分页需求我们可以使用form和size的方式实现,但是这种分页方式在深度分页的场景下应该是要避免使用的.深度分页会随着请求的页次增加,所消耗的内存和时间的增长也是成比例 ...
- ubuntu开发c/c++帮助文档
1.C语言库函数基本的帮助文档 sudo apt-get install manpages sudo apt-get install manpages-de sudo apt-get install ...
- ssh-add - 向认证代理添加 RSA 或 DSA 身份数据
总览 (SYNOPSIS) ssh-add [-lLdDx ] [-t life ] [file ... ] ssh-add -s reader ssh-add -e reader 描述 (DESCR ...
- 基于ZYNQ XC7Z045 FFG 900的高性能计算模块
一.板卡概述 本板卡基于Xilinx公司的FPGA XC7Z045 FFG 9000 芯片, 该平台为设计和验证应用程序提供了一个完整的开发平台.该平台使设计师能够更加简单进行高性能的原型设计,并且通 ...
- vue,一路走来(13)--vue微信分享
vue微信分享 今天记录一下vue微信分享. 1.先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”.这个不多说,见文档,只有绑定了才能进行下一步的动作 2.需要引入js文件 ...
- Git--03 git分支
目录 Git分支 1.新建testing分支 2.合并分支 3.合并冲突 4.删除分支 Git标签使用 1.查看标签 02.删除标签 Git分支 分支即是平行空间,假设你在为某个手机系统研发拍照功 ...
- HIbernate 查询拼接参数
public List<TrailTestModel> findByEid(List<String> trailids, String eid) { // TODO Auto- ...
- ivew 双向绑定时间控件
<FormItem label="开始时间" style="width: 100%" prop="startDate"> < ...
- 前后端分离下的CAS跨域流程分析
写在最前 前后端分离其实有两类: 开发阶段使用dev-server,生产阶段是打包成静态文件整个放入后端项目中. 开发阶段使用dev-server,生产阶段是打包成静态文件放入单独的静态资源服务器中, ...