1,spring获取资源文件的集中方法:
【1】File file = new File("fileSystemConfig.xml");
Resource resource = new FileSystemResource(file);
BeanFactory beanFactory = new XmlBeanFactory(resource);
【2】Resource resource = new ClassPathResource("classpath.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
【3】ClassPathXmlApplicationContext:ApplicationContext实现,从classpath获取配置文件;
BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath.xml");
【4】FileSystemXmlApplicationContext:ApplicationContext实现,从文件系统获取配置文件。
BeanFactory beanFactory = new FileSystemXmlApplicationContext("fileSystemConfig.xml");
2,spring构造函数注入的三种方式:
一、根据参数索引注入,使用标签“<constructor-arg index="1" value="1"/>”来指定注入的依赖,其中“index”表示索引,从0开始,即第一个参数索引为0,“value”来指定注入的常量值。
二、根据参数类型进行注入,使用标签“<constructor-arg type="java.lang.String" value="Hello World!"/>”来指定注入的依赖,其中“type”表示需要匹配的参数类型,可以是基本类型也可以是其他类型,如“int”、“java.lang.String”,“value”来指定注入的常量值。
三、根据参数名进行注入,使用标签“<constructor-arg name="message" value="Hello World!"/>”来指定注入的依赖,其中“name”表示需要匹配的参数名字,“value”来指定注入的常量值
3,spring配置的一些基础
一、构造器注入:
1)常量值
简写:<constructor-arg index="0" value="常量"/>
全写:<constructor-arg index="0"><value>常量</value></constructor-arg>
2)引用
简写:<constructor-arg index="0" ref="引用"/>
全写:<constructor-arg index="0"><ref bean="引用"/></constructor-arg>
二、setter注入:
1)常量值
简写:<property name="message" value="常量"/>
全写:<property name="message"><value>常量</value></ property>
2)引用
简写:<property name="message" ref="引用"/>
全写:<property name="message"><ref bean="引用"/></ property>
3)数组:<array>没有简写形式
4)列表:<list>没有简写形式
5)集合:<set>没有简写形式
6)字典
简写:<map>
<entry key="键常量" value="值常量"/>
<entry key-ref="键引用" value-ref="值引用"/>
</map>
全写:<map>
<entry><key><value>键常量</value></key><value>值常量</value></entry>
<entry><key><ref bean="键引用"/></key><ref bean="值引用"/></entry>
</map>
7)Properties:没有简写形式
3,对于spring的apect的一些疑问
第一个aop开始:
package com.duduli.li;
public interface IHelloWorldService {
public void sayHello();
}
实现接口:
在这个方法进行切入
package com.duduli.li;
public class HelloWorldService implements IHelloWorldService {
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("============Hello World!");
}
}
这个类是切入方法类
package com.duduli.li;
public class HelloWorldAspect {
public void beforeAdvice() {
System.out.println("===========before advice");
}
// 后置最终通知
public void afterFinallyAdvice() {
System.out.println("===========after finally advice");
}
}
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="helloWorldService" class="com.duduli.li.HelloWorldService"/>
<bean id="aspect" class="com.duduli.li.HelloWorldAspect">
</bean>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.duduli.li..*.*(..))" />
<aop:aspect ref="aspect">
<aop:before pointcut-ref="pointcut" method="beforeAdvice"/>
<aop:after pointcut-ref="pointcut" method="afterFinallyAdvice"/>
</aop:aspect>
</aop:config>
</beans>
然后在实现测试方法的时候,发现采用
Resource res = new ClassPathResource("bean.xml");
不能实现切入。
@Test
public void test01(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
IHelloWorldService helloworldService =
(IHelloWorldService) ctx.getBean("helloWorldService");
helloworldService.sayHello();
//下面的方法是没有进行切入的,不知道为什么?
// Resource res = new ClassPathResource("bean.xml");
// BeanFactory factory = new XmlBeanFactory(res);
// IHelloWorldService helloworldService2 =
// (IHelloWorldService) factory.getBean("helloWorldService");
// helloworldService2.sayHello();
}
}
4,使用注解形式的Aspect。
不多说maven的pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.0</version>
</dependency>
需要切面的方法:
package com.duduli.li;
public class HelloWorld {
public void sayHello(){
System.out.println("Hello ");
}
public void sayHello(String name){
System.out.println("Hello " + name);
}
}
package com.duduli.li;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
@org.aspectj.lang.annotation.Aspect
public class Aspect {
@Before("execution(* *..* (String))")
public void beforePointcut(){
System.out.println("before...");
}
@After("execution(* com.duduli.li..* (..))")
public void afterPointcut(){
System.out.println("after...");
}
}
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy/>
<bean id="hello" class="com.duduli.li.HelloWorld"/>
<bean id="aspect" class="com.duduli.li.Aspect">
</bean>
</beans>
测试类:
@Test
public void testAnnotationBeforeAdvice() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean2.xml");
HelloWorld hw =
ctx.getBean("hello", HelloWorld.class);
hw.sayHello();
hw.sayHello("duduli");
}
很简单的。没有任何侵入,按照这样的思想,我们可以做日志系统。权限系统(过滤)。
分享到:
相关推荐
spring一些常用知识点spring一些常用知识点 spring一些常用知识点 spring一些常用知识点spring一些常用知识点spring一些常用知识点
spring阶段知识点总结
spring的知识整理和框架的结合开发。 IOC 控制反转(Inversion of Control)、 依赖注入(Dependency Injection)等基础知识的详细讲解。
Spring文档,包括spring知识点总结,对初学者学习spring却有帮助
Spring基础知识汇总 Java开发必看
Spring : 春天 —>给软件行业带来了春天 2002年,Rod Jahnson首次推出了Spring框架雏形interface21框架。 2004年3月24日,Spring框架以interface21框架为基础,经过重新设计,发布了1.0正式版。 很难想象Rod ...
关于Spring Cloud知识点而整理出的思维导图,可以快速了解其使用方式,其中包括服务管理、消息代理、网关等知识点
spring从搭建配置到简单开发的一个整体流程,比较适合打基础的朋友!
三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot知识点.pdf三十五个常见的Spring Boot...
三十五个常见的Spring Boot知识点.docx三十五个常见的Spring Boot知识点.docx三十五个常见的Spring Boot知识点.docx三十五个常见的Spring Boot知识点.docx三十五个常见的Spring Boot知识点.docx三十五个常见的Spring...
spring AOP 理论知识点总结.wpsspring AOP 理论知识点总结.wpsspring AOP 理论知识点总结.wps
Spring基础知识汇总Java开发必看
springcloud汇总知识点,从零到精通springcloud汇总知识点,从零到精通springcloud汇总知识点,从零到精通springcloud汇总知识点,从零到精通springcloud汇总知识点,从零到精通springcloud汇总知识点,从零到精通...
Spring Cloud 知识点暨面试题
以思维导图的形式对Spring的基础理论知识进行一个汇总,思维导图中包含Spring简介、背景、特征、特点、优点、缺点及其Spring版本迭代做了详细介绍。
Spring核心知识点
Spring基础知识详解
纯文档的spring 知识点讲解,快速学会spring 原理讲解,配置讲解,具体实现讲解,出错排除讲解。
SpringCloud系列Demo代码,每个子项目都是SpringCloud的一个知识点或者说技能点且都有对应的博客介绍,代码开箱即用适合新手学习或老司机复习。 SpringCloud系列Demo代码,每个子项目都是SpringCloud的一个知识点...
学习spring时的知识点练习代码,有说明,欢迎下载