【spring】一些知识
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");一、根据参数索引注入,使用标签“<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”来指定注入的常量值
一、构造器注入: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:没有简写形式
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 stubSystem.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 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" 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");不能实现切入。@Testpublic 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();}}<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.Aspectpublic 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" name="code">@Test public void testAnnotationBeforeAdvice() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bean2.xml"); HelloWorld hw = ctx.getBean("hello", HelloWorld.class); hw.sayHello(); hw.sayHello("duduli");}