7.6 Spring 3.0 提供的Java配置管理

      Spring 允许使用Java类进行配置管理,可以不使用XML来管理Bean,以及Bean之间的依赖关系。

      Interface :Person

package edu.pri.lime._7_6.bean;

public interface Person {

    void userAxe();

}

      Interface : Axe

package edu.pri.lime._7_6.bean;

public interface Axe {

    String chop();
}

      Class : Chinese

package edu.pri.lime._7_6.bean.impl;

import edu.pri.lime._7_6.bean.Axe;
import edu.pri.lime._7_6.bean.Person; public class Chinese 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 userAxe() {
System.out.println("我是:" + name + "," + axe.chop());
}
}

      Class : StoneAxe

package edu.pri.lime._7_6.bean.impl;

import edu.pri.lime._7_6.bean.Axe;

public class StoneAxe implements Axe {

    public String chop() {
return "石斧砍柴真慢";
} }

      Class : SteelAxe

package edu.pri.lime._7_6.bean.impl;

import edu.pri.lime._7_6.bean.Axe;

public class SteelAxe implements Axe {

    public String chop() {
return "钢斧砍柴真快";
} }

      Class : AppConfig

package edu.pri.lime._7_6.bean.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import edu.pri.lime._7_6.bean.Axe;
import edu.pri.lime._7_6.bean.Person;
import edu.pri.lime._7_6.bean.impl.Chinese;
import edu.pri.lime._7_6.bean.impl.SteelAxe;
import edu.pri.lime._7_6.bean.impl.StoneAxe; //指定类为配置类
@Configuration
public class AppConfig { // 相当于定义一个名为personName的变量,其值为“lime”
@Value("lime")
String personName;
// 配置Bean : stoneAxe
@Bean(name="stoneAxe")
public Axe stoneAxe(){
return new StoneAxe();
}
// 配置Bean : steelAxe
@Bean(name="steelAxe")
public Axe steelAxe(){
return new SteelAxe();
}
// 配置一个Bean:chinese
@Bean(name="chinese")
public Person chinese(){
Chinese person = new Chinese();
person.setName(personName);
person.setAxe(steelAxe());
return person;
}
}

      Class :BeanTest

package edu.pri.lime._7_6.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import edu.pri.lime._7_6.bean.Person;
import edu.pri.lime._7_6.bean.configuration.AppConfig;
import edu.pri.lime._7_6.bean.impl.Chinese; public class BeanTest { public static void main(String[] args){
//使用Java配置类管理Spring容器中的Bean及其依赖关系时,使用AnnotationConfigApplication创建容器
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Person chinese = ctx.getBean("chinese",Chinese.class);
chinese.userAxe();
}
}

      @Configuration : 用于修饰一个Java配置类

      @Bean : 用于修饰一个方法,将该方法的返回值定义成容器中的一个Bean。

      @Value : 用于修饰以恶搞Field,用于为该Field配置一个值,相当于配置一个变量。

      @Import : 修饰一个Java配置类,用于向当前Java配置类中导入其他Java配置类。

      @Scope : 用于修饰一个方法,指定该方法对应的Bean的生命域。

      @Lazy : 用于修饰一个方法,指定该方法对应的Bean是否需要延迟初始化。

      @DependsOn : 用于修饰一个方法,指定在初始化该方法对应的Bean之前初始化指定的Bean。

      在实际项目中可能会混合使用XML配置和Java类配置混合使用:

      ⊙ 如果以XML配置为主,就需要让XMLpehiz能加载Java类配置。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<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">
<!-- 加载Java配置类 -->
<bean class="edu.pri.lime._7_6.bean.configuration.AppConfig" />
</beans>

        由于以XML配置为主,因此应用创建Spring容器时,还是以XML配置文件为参数来创建ApplicationContext对象。那么Spring会先加载XML配置文件,在根据XML配置文件的指示去加载指定的Java配置类。

      ⊙ 如果以Java类配置为主,就需要让Java配置类能加载XML配置。在配置类上增加@ImportResource注解,修饰Java配置类,用于导入指定的XML配置文件。

package edu.pri.lime._7_6.bean.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource; import edu.pri.lime._7_6.bean.Axe;
import edu.pri.lime._7_6.bean.Person;
import edu.pri.lime._7_6.bean.impl.Chinese;
import edu.pri.lime._7_6.bean.impl.SteelAxe;
import edu.pri.lime._7_6.bean.impl.StoneAxe; //指定类为配置类
@Configuration
@ImportResource(value = { "classpath:app_7_6.xml" })
public class AppConfig { // 相当于定义一个名为personName的变量,其值为“lime”
@Value("lime")
String personName;
// 配置Bean : stoneAxe
@Bean(name="stoneAxe")
public Axe stoneAxe(){
return new StoneAxe();
}
// 配置Bean : steelAxe
@Bean(name="steelAxe")
public Axe steelAxe(){
return new SteelAxe();
}
// 配置一个Bean:chinese
@Bean(name="chinese")
public Person chinese(){
Chinese person = new Chinese();
person.setName(personName);
person.setAxe(steelAxe());
return person;
}
}

      由于以Java类配置为主,因此应用创建Spring容器时,应以Java配置类为参数,通过创建AnnotationConfigApplicationContext对象作为Spring容器。那么Spring会先加载Java配置类,在根据Java配置类的指示去加载指定的XML配置文件。

7 -- Spring的基本用法 -- 6... Spring 3.0 提供的Java配置管理的更多相关文章

  1. 7 -- Spring的基本用法 -- 12... Spring 3.0 提供的表达式语言(SpEL)

    7.12 Spring 3.0 提供的表达式语言(SpEL) Spring表达式语言(简称SpEL)是一种与JSP 2 的EL功能类似的表达式语言,它可以在运行时查询和操作对象图.支持方法调用和基本字 ...

  2. 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;

    7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...

  3. 7 -- Spring的基本用法 -- 3... Spring 的核心机制 : 依赖注入

    7.3 Spring 的核心机制 : 依赖注入 Spring 框架的核心功能有两个. Spring容器作为超级大工厂,负责创建.管理所有的Java对象,这些Java对象被称为Bean. Spring容 ...

  4. 7 -- Spring的基本用法 -- 6...

    7.6 Spring 3.0 提供的Java配置管理 Spring 允许使用Java类进行配置管理,可以不使用XML来管理Bean,以及Bean之间的依赖关系. Interface :Person p ...

  5. SpringMVC +mybatis+spring 结合easyui用法及常见问题总结

    SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...

  6. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

  7. spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情

    <spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...

  8. Spring中@Async用法详解及简单实例

    Spring中@Async用法 引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类 ...

  9. Spring.Net简单用法

    Spring.Net其实就是抽象工厂,只不过更加灵活强大,性能上并没有明显的区别. 它帮我们实现了控制反转. 其有两种依赖注入方式. 第一:属性注入 第二:构造函数注入 首先,我们去  Spring. ...

随机推荐

  1. Spring Cloud 获取注册中心所有服务以及服务下的所有实例

    注册中心现有服务与实例数: 在任意客户端填写如下代码: /** * import org.springframework.cloud.client.ServiceInstance; * import ...

  2. springboot项目访问不到controller方法。

    访问不到方法首先要从你的controller能否被扫描到出发, 图中显示创建springboot项目自带的这两个的文件要注意把他俩拿出来放到父包下面也就是图中这个位置.如果你的这两个文件在子包里或者说 ...

  3. MFC函数—— CWnd::PreCreateWindow

     CWnd::PreCreateWindow virtual BOOL PreCreateWindow( CREATESTRUCT& cs ); 返回值: 如果要继续窗口的创建过程,则返回非零 ...

  4. 自然语言交流系统 phxnet团队 创新实训 项目博客 (三)

    语音转文本部分是调用的科大讯飞的在线语音,它的激发方式是按键,通过按钮触发开启安卓设备的录音,此部分需要在源码中写入关于安卓权限的要求,来调用安卓的录音权限,当按钮被激发,则开始进入语音录制阶段,将麦 ...

  5. 技能UP:SAP OBYC自动记账的实例说明(含value String应用说明)

    一. 自动过账原理 在MM模块的许多操作都能实现在FI模块自动过账,如PO收货.发票验证(LIV).工单发料.向生产车间发料等等.不用说,一定需要在IMG中进行配置才可以实现自动处理.但SAP实现的这 ...

  6. (笔记)Mysql实例:建库建表并插入数据1

    drop database if exists school;  // 如果存在school则删除create database school;  // 建立库schooluse school;  / ...

  7. 计算机网络——链路层协议

    一. 链路层的功能 可靠交付:在高差错的链路,如无线链路,可以进行可靠交付:对于其它的有线,可以是多余的: 流量控制:防止接收方的缓存区溢出,帧丢失: 差错检测与差错纠正:在硬件上实现了: 二.多路访 ...

  8. 用OpenGL实现跳跃的立体小球

    一.目的 掌握OpenGL中显示列表对象的使用方法. 二.示例代码 Github地址 #include "stdafx.h" #include <GL/glut.h> ...

  9. Prolog学习:数独和八皇后问题

    上一篇简单介绍了下Prolog的一些基本概念,今天我们来利用这些基本概念解决两个问题:数独和八皇后问题. 数独 数独是一个很经典的游戏: 玩家需要根据n×n盘面上的已知数字,推理出所有剩余空格的数字, ...

  10. JDBC插入数据实例

    在本教程将演示如何在JDBC应用程序中向数据库的一个表中插入数据记录. 在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员权限,以在给定模式中数据库表中插入数据记录. 要执行以下示例, ...