Spring基于java注解配置

来源:互联网 发布:华为云计算认证 编辑:程序博客网 时间:2024/06/04 18:19


Spring框架是控制反转 (IOC) 或依赖性注入 (DI) 模式的推动因素,而这种推动是通过基于容器的配置实现的。过去,
Spring 允许开发人员使用基于 XML 的配置,通过利用应用程序上下文 XML 文件来管理 bean 依赖性。此文件处于应用程序的外部,包含 bean 及其与该应用程序的依赖项的定义。尽管使用 XML 配置较为简单和便捷,但仍有另外一种方法可定义bean 及其依赖项。这种方法也称为基于 Java 的配置。不同于 XML,基于 Java 的配置能够以编程方式管理bean。这可通过运用多种注释来实现。

----------------------------------------------

 基于java注解配置:


带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。
@Bean 注解告诉 Spring,一个带有 @Bean的注解方法将返回一个对象,该对象应该被注册为在Spring 应用程序上下文中的bean
@Value:用于修饰一个Field,用于为该Field配置一个值,相当于配置一个变量。
@Import:修饰一个Java配置类,用于向当前Java配置类中导入其他Java配置类
@Scope:用于修饰一个方法,指定该方法对应的Bean的生命域
@Lazy:用于修饰一个方法,指定该方法对应的Bean是否需要延迟初始化
@DependsOn:用于修饰一个方法,指定在初始化该方法对应的Bean之前初始化指定的Bean
@ImportResource注:用于修饰Java配置类,用于导入指定的XML配置文件

--------------------------------------------

具体代码如下
java配置类

@Configuration
public class AppConfig {
// 相当于定义一个名为personName的变量,其值为"泰山"
@Value("泰山") String personName;
@Bean(name="chinese")
public Person person()
{
Chinese p=new Chinese();
p.setName(personName);
p.setAxe(steelAxe());
return p;
}
// 配置Bean:stoneAxe
@Bean(name="stoneAxe")
public Axe stoneAxe(){
return new StoneAxe();

}
// 配置Bean:steelAxe
@Bean(name="steelAxe")
public Axe steelAxe()
{
return new SteelAxe();
}
}

service层:

package com.cn.service;

public interface Axe {
   public String chop();
}

package com.cn.service;

public interface Person {
public void useAxe();
}


service.imp

package com.cn.service.imp;


import com.cn.service.Axe;
import com.cn.service.Person;

public class America implements Person {
private Axe axe;
private String name;

public Axe getAxe() {
return axe;
}

public void setAxe(Axe axe) {
this.axe = axe;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public void useAxe() {
System.out.println("我是木星人:名字为:" + name + "。正在用斧头" + axe.chop());
}
}

package com.cn.service.imp;


import com.cn.service.Axe;


public class SteelAxe implements Axe {

@Override
public String chop() {
// TODO Auto-generated method stub
return "刚斧头砍树真快";
}
}

测试类

package com.cn.text;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cn.service.Person;

public class BeanText {
 public static void main(String[] args) {
ApplicationContext acContext=new ClassPathXmlApplicationContext("bean.xml");
Person person=acContext.getBean("chinese",Person.class);
person.useAxe();
Person person2=acContext.getBean("america",Person.class);
person2.useAxe();
}
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="stoneAxe" class="com.cn.service.imp.StoneAxe" />
<bean id="steelAxe" class="com.cn.service.imp.SteelAxe" />
<!-- 此处是与bean继承结合简化代码 -->
<bean id="pt" abstract="true" class="com.cn.service.imp.Chinese">
<property name="name" value="泰山"></property>
<property name="axe" ref="steelAxe"></property>
</bean>
 
<bean id="chinese" parent="pt" />
<bean id="america" class="com.cn.service.imp.America" parent="pt">
<property name="axe" ref="stoneAxe"></property>
</bean>
</beans>
基于java注解配置

基于java注解配置


原创粉丝点击