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"/>
相关文章
|
XML 安全 Java
使用 Spring 的 @Aspect 和 @Pointcut 注解简化面向方面的编程 (AOP)
面向方面编程(AOP)通过分离横切关注点,如日志、安全和事务,提升代码模块化与可维护性。Spring 提供了对 AOP 的强大支持,核心注解 `@Aspect` 和 `@Pointcut` 使得定义切面与切入点变得简洁直观。`@Aspect` 标记切面类,集中处理通用逻辑;`@Pointcut` 则通过表达式定义通知的应用位置,提高代码可读性与复用性。二者结合,使开发者能清晰划分业务逻辑与辅助功能,简化维护并提升系统灵活性。Spring AOP 借助代理机制实现运行时织入,与 Spring 容器无缝集成,支持依赖注入与声明式配置,是构建清晰、高内聚应用的理想选择。
1069 0
|
XML Java 数据格式
《深入理解Spring》:AOP面向切面编程深度解析
Spring AOP通过代理模式实现面向切面编程,将日志、事务等横切关注点与业务逻辑分离。支持注解、XML和编程式配置,提供五种通知类型及丰富切点表达式,助力构建高内聚、低耦合的可维护系统。
|
人工智能 监控 安全
Spring AOP切面编程颠覆传统!3大核心注解+5种通知类型,让业务代码纯净如初
本文介绍了AOP(面向切面编程)的基本概念、优势及其在Spring Boot中的使用。AOP作为OOP的补充,通过将横切关注点(如日志、安全、事务等)与业务逻辑分离,实现代码解耦,提升模块化程度、可维护性和灵活性。文章详细讲解了Spring AOP的核心概念,包括切面、切点、通知等,并提供了在Spring Boot中实现AOP的具体步骤和代码示例。此外,还列举了AOP在日志记录、性能监控、事务管理和安全控制等场景中的实际应用。通过本文,开发者可以快速掌握AOP编程思想及其实践技巧。
|
人工智能 监控 安全
如何快速上手【Spring AOP】?核心应用实战(上篇)
哈喽大家好吖~欢迎来到Spring AOP系列教程的上篇 - 应用篇。在本篇,我们将专注于Spring AOP的实际应用,通过具体的代码示例和场景分析,帮助大家掌握AOP的使用方法和技巧。而在后续的下篇中,我们将深入探讨Spring AOP的实现原理和底层机制。 AOP(Aspect-Oriented Programming,面向切面编程)是Spring框架中的核心特性之一,它能够帮助我们解决横切关注点(如日志记录、性能统计、安全控制、事务管理等)的问题,提高代码的模块化程度和复用性。
1048 1
|
设计模式 Java 开发者
如何快速上手【Spring AOP】?从动态代理到源码剖析(下篇)
Spring AOP的实现本质上依赖于代理模式这一经典设计模式。代理模式通过引入代理对象作为目标对象的中间层,实现了对目标对象访问的控制与增强,其核心价值在于解耦核心业务逻辑与横切关注点。在框架设计中,这种模式广泛用于实现功能扩展(如远程调用、延迟加载)、行为拦截(如权限校验、异常处理)等场景,为系统提供了更高的灵活性和可维护性。
1690 0
|
监控 Java Spring
AOP 切面编程
AOP(面向切面编程)通过动态代理在不修改源码的前提下,对方法进行增强。核心概念包括连接点、通知、切入点、切面和目标对象。常用于日志记录、权限校验、性能监控等场景,结合Spring AOP与@Aspect、@Pointcut等注解,实现灵活的横切逻辑管理。
2788 6
AOP 切面编程
Micronaut AOP与代理机制:实现应用功能增强,无需侵入式编程的秘诀
AOP(面向切面编程)能够帮助我们在不修改现有代码的前提下,为应用程序添加新的功能或行为。Micronaut框架中的AOP模块通过动态代理机制实现了这一目标。AOP将横切关注点(如日志记录、事务管理等)从业务逻辑中分离出来,提高模块化程度。在Micronaut中,带有特定注解的类会在启动时生成代理对象,在运行时拦截方法调用并执行额外逻辑。例如,可以通过创建切面类并在目标类上添加注解来记录方法调用信息,从而在不侵入原有代码的情况下增强应用功能,提高代码的可维护性和可扩展性。
443 2
|
监控 Java Spring
AOP切面编程快速入门
AOP(面向切面编程)通过分离共性逻辑,简化代码、减少冗余。它通过切点匹配目标方法,在不修改原方法的前提下实现功能增强,如日志记录、性能监控等。核心概念包括:连接点、通知、切入点、切面和目标对象。Spring AOP支持多种通知类型,如前置、后置、环绕、返回后、异常通知,灵活控制方法执行流程。通过@Pointcut可复用切点表达式,提升维护性。此外,结合自定义注解,可实现更清晰的切面控制。
954 5
|
人工智能 监控 Java
面向切面编程(AOP)介绍--这是我见过最易理解的文章
这是我见过的最容易理解的文章,由浅入深介绍AOP面向切面编程,用科普版和专家版分别解说,有概念,有代码,有总结。
|
安全 Java 编译器
什么是AOP面向切面编程?怎么简单理解?
本文介绍了面向切面编程(AOP)的基本概念和原理,解释了如何通过分离横切关注点(如日志、事务管理等)来增强代码的模块化和可维护性。AOP的核心概念包括切面、连接点、切入点、通知和织入。文章还提供了一个使用Spring AOP的简单示例,展示了如何定义和应用切面。
2010 1
什么是AOP面向切面编程?怎么简单理解?