引用
Spring 方法替换例子
1.sec3.xml中
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<bean id="tigerReplacer" class="com.lh.springtest02.section3.TigerReplacer" />
<bean id="magicbox" class="com.lh.springtest02.section3.MagicBoxImpl">
<replaced-method name="getContents" replacer="tigerReplacer"/>
</bean>
<bean id="harry" class="com.lh.springtest02.section3.Magician">
<property name="magicWords" value="Bippity boppity boo" />
<property name="magicBox" ref="magicbox" />
</bean>
</beans>
2.替换方法实现
import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;
public class TigerReplacer implements MethodReplacer{
public Object reimplement(Object obj, Method method, Object[] args)
throws Throwable {
return "A ferocious tiger";
}
}
3.MagicBoxImpl.java
public class MagicBoxImpl implements MagicBox{
public MagicBoxImpl() {
}
public String getContents() {
return "A Beautiful assistant";
}
}
4.Magician.java
public class Magician implements Performer{
private MagicBox magicBox;
private String magicWords;
public void perform() {
System.out.println(magicWords);
System.out.println("The magic box contains ...");
System.out.println(magicBox.getContents());
}
public void setMagicBox(MagicBox magicBox) {
this.magicBox = magicBox;
}
public void setMagicWords(String magicWords) {
this.magicWords = magicWords;
}
}
5.Bean测试
ApplicationContext ac = new ClassPathXmlApplicationContext("section3/sec3.xml");
Magician mc = (Magician)ac.getBean("harry");
mc.perform();
分享到:
相关推荐
<artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> --------------------------- src/main/resources/application.yml --------------------------- spring: # 指定...
总结,Maven+SpringMVC的组合为Java Web开发提供了强大的支持,从项目管理到应用构建,再到复杂的业务逻辑处理,它们都扮演着不可或缺的角色。通过这个入门级的HelloWorld实例,我们可以快速理解这两个工具的基本...
Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境
### Maven+SSM(Spring+SpringMVC+MyBatis)整合框架详解 #### Maven 安装与配置 在深入SSM框架整合之前,首先需要确保Maven环境的正确安装与配置。 1. **安装Maven** - **前提条件**:安装 JDK 1.7 及以上版本并...
### Spring MVC + Spring + MyBatis + Maven + MySQL 环境搭建详解 #### 一、概述 在软件开发领域,尤其是Java Web开发中,**Spring MVC + Spring + MyBatis + Maven + MySQL**组合是一种非常流行的开发模式。本文...
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.x.x.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>...
<artifactId>springMybaits</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>springMybaits</name> <url>http://maven.apache.org</url> <properties> <project.build....
### 基于Maven的Spring + Spring MVC + Mybatis 的环境搭建 #### 一、Maven简介与优势 Maven是一种项目管理和构建工具,它主要用于Java项目的构建、依赖管理和项目文档生成等任务。Maven的优势在于其强大的依赖...
### 基于Maven的Spring + Spring MVC + MyBatis环境搭建详解 #### 一、项目背景与目标 在本篇文章中,我们将探讨如何基于Maven构建一个包含Spring、Spring MVC以及MyBatis框架的集成环境。项目的主要目的是为了...
<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.18</version> </dependency> <!-- Spring Core --> <dependency> <groupId>org.springframework</groupId> ...
<groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.23</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api...
--<artifactId>mybatis-generator-maven-plugin</artifactId>--> <!--<version>1.3.5</version>--> <!--<configuration>--> <!--<verbose>true</verbose>--> <!--<overwrite>true</overwrite>--> <!--</...
<artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` 继承 Starter 父 POM Spring Boot 提供了一个 Starter 父 POM,用于简化 Spring Boot 项目的构建过程。该 POM 包含了...
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 数据源配置 --> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean...
【标题】:“使用IntelliJ IDEA搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)项目” 【正文】 在Java开发领域,Spring、SpringMVC和MyBatis框架的组合,简称SSM,是构建Web应用程序的常用技术栈。IntelliJ IDEA作为...
<property name="url" value="jdbc:mysql:///maven"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!--配置生产SqlSession对象的工厂--> <bean id=...
<spring.version>4.3.0.RELEASE</spring.version> <hibernate.version>5.2.0.Final</hibernate.version> <struts2.version>2.5.1</struts2.version> <jackson.version>2.7.5</jackson.version> <log4j.version>...
<groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.x.x.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>...
Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/
【标题】"maven+spring+jdbc+mvc项目整合"是一个综合性的开发教程,它将四个关键的技术元素——Maven、Spring、JDBC和Model-View-Controller(MVC)框架融合在一个项目中,旨在帮助开发者理解如何在实际开发环境中...