IOC简单例子
1.新建普通JAVA工程SpringDemo1,工程新建lib目录,把以下JAR包复制到该目录下:spring-core.jar,commons-logging.jar
2.新建IocService,内容如下:
package com.ioc;
public class IocService {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void display(){
System.out.println(message);
}
}
3.新建IocBean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="iocService" class="com.ioc.IocService">
<property name="message" value="hello world!"/>
</bean>
</beans>
4.新建测试类IocMain,内容如下:
package com.ioc;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class IocMain {
public static void main(String[] args) {
//Resource resource=new FileSystemResource("src/com/ioc/IocBean.xml");
//上面这种方式也可以
ClassPathResource resource=new ClassPathResource("com/ioc/IocBean.xml");
BeanFactory factory=new XmlBeanFactory(resource);
IocService iocService=(IocService) factory.getBean("iocService");
iocService.display();
}
}
AOP简单例子
1.工程中导入以下导:aopalliance.jar,aspectjrt.jar,aspectjweaver.jar,cglib-nodep-2.1_3.jar,spring-aop.jar,spring-context.jar.
2.新建类LoggerBean,内容如下:
package com.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggerBean {
public Object aroundLogCalls(ProceedingJoinPoint point) throws Throwable{
System.out.println("befor method:"+point.getSignature().getName());
Object result=point.proceed();
System.out.println("after method:"+point.getSignature().getName());
return result;
}
}
3.新建类AopBean,内容如下:
package com.aop;
public class AopBean {
public void display(){
System.out.println("AopBean display...");
}
}
4.新建AopBean.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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:config>
<aop:pointcut id="loggerCalls" expression="execution(public * *(..)) and
!execution(* com.aop.LoggerBean.*(..))"/>
<aop:aspect id="logAspect" ref="loggerBean">
<aop:around pointcut-ref="loggerCalls" method="aroundLogCalls"/>
</aop:aspect>
</aop:config>
<bean id="loggerBean" class="com.aop.LoggerBean"/>
<bean id="aopBean" class="com.aop.AopBean" />
</beans>
5.新建测试类AopMain,内容如下:
package com.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class AopMain {
public static void main(String[] args) {
// ClassPathResource resource=new ClassPathResource("com/aop/AopBean.xml");
// BeanFactory factory=new XmlBeanFactory(resource);
//用上面这种方法得到factory,就不会LoggerBean的aroundLogCalls方法
ClassPathXmlApplicationContext factory=new ClassPathXmlApplicationContext("com/aop/AopBean.xml");
AopBean aopBean=(AopBean) factory.getBean("aopBean");
aopBean.display();
}
}
6.出现错误:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting '(' at character position 0
loggerCalls
原因是AopBean.xml中<aop:around pointcut-ref="loggerCalls" method="aroundLogCalls"/>
写成<aop:around pointcut="loggerCalls" method="aroundLogCalls"/>

960

被折叠的 条评论
为什么被折叠?



