spring注解开发:Configuration&Bean
2024-09-02 09:07:48
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了。
最新文章
- C#与数据库访问技术总结(十四)之DataAdapter对象
- linq to xml学习
- AWstat(linux下)
- G-sensor驱动分析
- hdu 4628 动态规划
- mysql中php生成唯一ID
- C# winCE连接SQL数据库
- Hibernate学习笔记(一):mycelipse建立项目流程(未完成)
- KVO 的使用和举例
- HDU 4873 ZCC Loves Intersection(可能性)
- 程序、计算机程序、java初论
- 地图开发GIS的应用有哪些?
- 简单搭建iOS开发项目框架
- 一个word文档中,多个表格的批量调整(根据窗口调整表格和添加表格水平线)
- 20175310 《Java程序设计》第6周学习总结
- SNF软件开发机器人-子系统-功能-数据录入方式
- Linux 命令查询工具
- Python3红楼梦人名出现次数统计分析
- ASP.NET MVC中如何以ajax的方式在View和Action中传递数据
- PowerShell实现基于SharePoint的网站HomePage Auto-Configure Solution
热门文章
- Edit the AlarmClock in AOSP with android-studio
- http响应代码解释
- [Luogu2365]任务安排(斜率优化)
- Hibernate 一对一(基于唯一外键的关联)
- springboot+jsp+mybatis项目实例(后台成功,但是无法跳转jsp页面,没有实体类的注解,看springboot+jsp第二弹相关配置,即可成功配置jsp)
- 【leetcode】1018. Binary Prefix Divisible By 5
- TypeError:NoneType object is not callable情况下的错误
- 跨域AJAX
- hdu 1451 Area in Triangle(计算几何 三角形)
- webbrowser控件显示word文档