【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");


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();
}

实现接口:
[b]在这个方法进行切入[/b]
package com.duduli.li;

public class HelloWorldService implements IHelloWorldService {
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("============Hello World!");
}
}

[b]这个类是切入方法类[/b]
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");
}


很简单的。没有任何侵入,按照这样的思想,我们可以做日志系统。权限系统(过滤)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值