spring入门了解

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"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值