Spring入门&控制反转(或依赖注入)&AOP的关键概念& 多配置文件&与web集成(二)

简介: Spring入门&控制反转(或依赖注入)&AOP的关键概念& 多配置文件&与web集成

2) Student

public class Student extends Person {
  private String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  @Override
  public String toString() {
    return "Demo [name=" + name + "]";
  }
}

3) 配置文件

<bean id="student" class="org.lisen.springdemo.model.Student" parent="person">
    <property name="name">
      <value>张三</value>
    </property>
  </bean>
  <bean id="person" class="org.lisen.springdemo.model.Person" abstract="true">
    <property name="phone">
      <value>139751878778</value>
    </property>
  </bean>

4) 通过Spring的ApplicationContext获取Bean

public class SpringDemo {
  public static void main(String[] args) {
    ApplicationContext cxt = new ClassPathXmlApplicationContext("spring.xml");
    Student student = (Student)cxt.getBean("student");
    System.out.println(student.getName());
    System.out.println(student.getPhone());
  }
}

4.4.3 使用有参数构造方法创建javaBean

constructor-arg

1) Worker

public class Worker extends Person {
  private String workName;
  public Worker(String workName) {
    this.workName = workName;
  }
  public String getWorkName() {
    return workName;
  }
  public void setWorkName(String workName) {
    this.workName = workName;
  }
}

2)配置文件

<bean id="worker" class="org.lisen.springdemo.model.Worker" parent="person">
    <constructor-arg name="workName">
      <value>王小</value>
    </constructor-arg>
    <!-- 第二种方式
    <constructor-arg index="0">
      <value>王小</value>
    </constructor-arg>
     -->
  </bean>

3) 通过Spring的ApplicationContext获取Bean

public class SpringDemo {
  public static void main(String[] args) {
    ApplicationContext cxt = new ClassPathXmlApplicationContext("spring.xml");
    Student student = (Student)cxt.getBean("student");
    System.out.println(student.getName());
    System.out.println(student.getPhone());
    //构造函数注入
    Worker worker = (Worker)cxt.getBean("worker");
    System.out.println(worker.getWorkName());
  }
}

4.4.4 init-method:指定bean的初始化方法

  1. 修改上面的Student类,加入一个init方法,注意该方法不能有参数
public class Student extends Person {
  private String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  @Override
  public String toString() {
    return "Demo [name=" + name + "]";
  }
  //init方法,不能有参数
  public void init() {
    System.out.println("Student init ....");
  }
}

2) 修改Student类对应的配置文件

<bean id="student" class="org.lisen.springdemo.model.Student" parent="person" init-method="init">
    <property name="name">
      <value>张三</value>
    </property>
  </bean>

3) 运行原来的测试用例,会发生后台打印“Student init ....“,表示init-method正常调用。

4.4.5 复杂属性的配置

1) JavaBean属性注入

<bean id="student" class="org.lisen.springdemo.model.Student" parent="person" init-method="init">
    <property name="name">
      <value>张三</value>
    </property>
    <property name="addr" ref="addr"/>
  </bean>
  <bean id="addr" class="org.lisen.springdemo.model.Addr">
    <property name="city">
      <value>长沙</value>
    </property>
  </bean>

java部分代码比较简单,请根据配置文件自行完成。

2)List或数组

声明属性

private int[] arr;
 private List list;

配置文件

  <property name="list">
      <list>
        <value>123</value>
        <value>456</value>
        <value>789</value>
        <value>asd</value>
      </list>
    </property>

3)Map

private Map map;

配置文件

 <property name="map">
      <map>
        <entry key="aa" value="123"/>
        <entry key="bb" value="456"/>
        <entry key="cc" value="789"/>
      </map>
    </property>

4.Properties

private Properties prop;

配置文件

  <property name="prop">
      <props>
        <prop key="dd">qwer</prop>
        <prop key="ee">tyu</prop>
        <prop key="ff">iop</prop>
      </props>
    </property>

5. 多配置文件

系统一般会被分成多个模块,可以为每个模块配置一个配置文件,便于管理,在版本控制软件中也可以减少冲突

spring-a.xml 假设为a模块的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  <bean id="student" class="org.lisen.springdemo.model.Student" parent="person" init-method="init">
    <property name="name">
      <value>张三</value>
    </property>
    <property name="addr" ref="addr"/>
  </bean>
  <bean id="addr" class="org.lisen.springdemo.model.Addr">
    <property name="city">
      <value>长沙</value>
    </property>
  </bean>
  <bean id="person" class="org.lisen.springdemo.model.Person" abstract="true">
    <property name="phone">
      <value>139751878778</value>
    </property>
  </bean>
  <bean id="worker" class="org.lisen.springdemo.model.Worker" parent="person">
    <constructor-arg name="workName">
      <value>王小</value>
    </constructor-arg>
    <!-- 第二种方式
    <constructor-arg index="0">
      <value>王小</value>
    </constructor-arg>
     -->
  </bean>
</beans>

spring.xml 为总的spring配置文件, 在总配置文件中导入其他模块即可。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
  <import resource="spring-a.xml"/>
</beans>

6. 与web集成

6.1 集成配置

在web.xml中加入如下配置:

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

6.2 获取配置的Bean

/**
 * 用于简化Bean的获取.
 * 
 * ApplicationContextAware接口:
 * 实现了这个接口的bean,当spring容器初始化的时候,会自动的将ApplicationContext注入进来
 * 
 * @author Administrator
 */
public final class SpringBeanUtil implements ApplicationContextAware {
  private SpringBeanUtil() {
  }
  private static ApplicationContext cxt;
  @Override
  public void setApplicationContext(ApplicationContext appContext) throws BeansException {
    cxt = appContext;
  }
  /**
   * 根据Bean的id来获取Bean对象
   * @param id 配置文件中的bean的id属性
   * @return Object
   */
  @SuppressWarnings("unchecked")
  public static <T> T getBean(String id) {
    return (T)cxt.getBean(id);
  }
} 

配置文件

     <bean 
class="org.lisen.springdemo.util.SpringBeanUtil"/>
相关文章
|
8月前
|
XML Java 开发者
Spring底层架构核心概念解析
理解 Spring 框架的核心概念对于开发和维护 Spring 应用程序至关重要。IOC 和 AOP 是其两个关键特性,通过依赖注入和面向切面编程实现了高效的模块化和松耦合设计。Spring 容器管理着 Beans 的生命周期和配置,而核心模块为各种应用场景提供了丰富的功能支持。通过全面掌握这些核心概念,开发者可以更加高效地利用 Spring 框架开发企业级应用。
255 18
|
9月前
|
设计模式 XML Java
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
本文详细介绍了Spring框架的核心功能,并通过手写自定义Spring框架的方式,深入理解了Spring的IOC(控制反转)和DI(依赖注入)功能,并且学会实际运用设计模式到真实开发中。
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
|
9月前
|
Java 开发者 微服务
Spring Boot 入门:简化 Java Web 开发的强大工具
Spring Boot 是一个开源的 Java 基础框架,用于创建独立、生产级别的基于Spring框架的应用程序。它旨在简化Spring应用的初始搭建以及开发过程。
318 7
Spring Boot 入门:简化 Java Web 开发的强大工具
|
9月前
|
Java 数据库 数据安全/隐私保护
轻松掌握Spring依赖注入:打造你的登录验证系统
本文以轻松活泼的风格,带领读者走进Spring框架中的依赖注入和登录验证的世界。通过详细的步骤和代码示例,我们从DAO层的创建到Service层的实现,再到Spring配置文件的编写,最后通过测试类验证功能,一步步构建了一个简单的登录验证系统。文章不仅提供了实用的技术指导,还以口语化和生动的语言,让学习变得不再枯燥。
167 2
|
10月前
|
开发框架 JavaScript 前端开发
TypeScript 是一种静态类型的编程语言,它扩展了 JavaScript,为 Web 开发带来了强大的类型系统、组件化开发支持、与主流框架的无缝集成、大型项目管理能力和提升开发体验等多方面优势
TypeScript 是一种静态类型的编程语言,它扩展了 JavaScript,为 Web 开发带来了强大的类型系统、组件化开发支持、与主流框架的无缝集成、大型项目管理能力和提升开发体验等多方面优势。通过明确的类型定义,TypeScript 能够在编码阶段发现潜在错误,提高代码质量;支持组件的清晰定义与复用,增强代码的可维护性;与 React、Vue 等框架结合,提供更佳的开发体验;适用于大型项目,优化代码结构和性能。随着 Web 技术的发展,TypeScript 的应用前景广阔,将继续引领 Web 开发的新趋势。
216 2
|
11月前
|
前端开发 JavaScript 开发者
探索现代Web前端技术:React框架入门
【10月更文挑战第9天】 探索现代Web前端技术:React框架入门
|
11月前
|
网络协议 安全 JavaScript
Web实时通信的学习之旅:WebSocket入门指南及示例演示
Web实时通信的学习之旅:WebSocket入门指南及示例演示
1802 0
|
11月前
|
Web App开发 Java 测试技术
一、自动化:web自动化。Selenium 入门指南:从安装到实践
一、自动化:web自动化。Selenium 入门指南:从安装到实践
209 0
|
4月前
|
Web App开发 前端开发 JavaScript
鸿蒙5开发宝藏案例分享---Web适配一多开发实践
这是一份实用的鸿蒙Web多设备适配开发指南,针对开发者在不同屏幕尺寸下的布局难题提供了解决方案。文章通过三大法宝(相对单位、媒体查询和窗口监听)详细介绍如何实现智能适配,并提供了多个实战案例,如宫格布局、对话框变形和自适应轮播图等。此外,还分享了调试技巧及工具推荐,帮助开发者快速上手并优化性能。最后鼓励读者实践探索,并提示更多官方资源等待发现。
|
6月前
|
关系型数据库 MySQL 数据库
基于Flink CDC 开发,支持Web-UI的实时KingBase 连接器,三大模式无缝切换,效率翻倍!
TIS 是一款基于Web-UI的开源大数据集成工具,通过与人大金仓Kingbase的深度整合,提供高效、灵活的实时数据集成方案。它支持增量数据监听和实时写入,兼容MySQL、PostgreSQL和Oracle模式,无需编写复杂脚本,操作简单直观,特别适合非专业开发人员使用。TIS率先实现了Kingbase CDC连接器的整合,成为业界首个开箱即用的Kingbase CDC数据同步解决方案,助力企业数字化转型。
1191 5
基于Flink CDC 开发,支持Web-UI的实时KingBase 连接器,三大模式无缝切换,效率翻倍!