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配置类:
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();
}
public interface Axe {
public String chop();
}
package com.cn.service;
public interface Person {
public void useAxe();
}
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;
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 "刚斧头砍树真快";
}
}
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();
}
}
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" />
<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="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注解配置
阅读全文
0 0
- Spring基于java注解配置
- Spring基于Java注解的配置
- Spring 基于XML配置 基于注解配置 基于JAVA类配置比较以及适用场景
- spring基于注解的配置
- Spring基于注解配置Bean
- Spring基于注解配置AOP
- Spring基于注解的配置
- spring基于注解的配置
- spring 基于注解的控制器配置
- Spring 基于注解零配置开发
- 基于Spring注解方式配置项目
- Spring之基于注解的容器配置
- 【Quartz】基于Spring注解方式配置Quartz
- 基于Spring注解方式配置Quartz
- Spring框架 基于注解配置(四)
- Spring简单配置二-基于注解方式
- 【Quartz】基于Spring注解方式配置Quartz
- [Spring]基于注解的形式配置Bean
- 页面对象模式 Page Object
- Netty 简例
- 九个Console命令,让 JS 调试更简单
- TensorFlow学习--tf.session.run()
- 数组置换(基础)
- Spring基于java注解配置
- 【bzoj4723】 [POI2017]Flappy Bird
- fineReport报表导出数据,日期不能按月汇总的解决方法
- 关于 ios 多图异步下载
- linux 修改yum源
- JSP&Servlet 过滤器
- 关于状态保持
- HTTP 协议中的 Transfer-Encoding
- HDU1279验证角谷猜想