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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="S" class="test.SImpl">
<property name="username" value="asdf"></property>
</bean>
<bean id="Loger" class="test.Loger"></bean>
<aop:config>
<aop:aspect ref="Loger">
<aop:pointcut id="LogPointcut" expression="execution(* *.getUsername(..)) and target(bean)"/>
<aop:before method="beforGetName" pointcut-ref="LogPointcut" arg-names="bean"/>
<aop:after method="afterGetName" pointcut-ref="LogPointcut" arg-names="bean"/>
</aop:aspect>
</aop:config>
</beans> ---------------
package test; public interface Int_S {
public String getUsername();
}
--------------------
package test; public class Loger {
public void beforGetName(Int_S s) {
System.out.println("Before get user name"+s.getUsername());
} public void afterGetName(Int_S s) {
System.out.println("After get user name"+s.getUsername());
}
}
----------------------------
package test; public class SImpl implements Int_S { private String username="hxf"; @Override
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} public String getN() {
// TODO Auto-generated method stub
return username;
} }
---------------------------------
package test; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource; public class Test {
public static void main(String[] d){
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src/beanRefFactory.xml"));
//BeanFactory factory = new ClassPathXmlApplicationContext("beanRefFactory.xml");
SImpl greetingServices =(SImpl)factory.getBean("S");
System.out.println( greetingServices.getUsername());
}
} 期望Loger能自动system out,.但是没反应..console 只输出 asdf 我希望greetingServices.getUsername() 的时候 loger能自动输出
<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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="S" class="test.SImpl">
<property name="username" value="asdf"></property>
</bean>
<bean id="Loger" class="test.Loger"></bean>
<aop:config>
<aop:aspect ref="Loger">
<aop:pointcut id="LogPointcut" expression="execution(* *.getUsername(..)) and target(bean)"/>
<aop:before method="beforGetName" pointcut-ref="LogPointcut" arg-names="bean"/>
<aop:after method="afterGetName" pointcut-ref="LogPointcut" arg-names="bean"/>
</aop:aspect>
</aop:config>
</beans> ---------------
package test; public interface Int_S {
public String getUsername();
}
--------------------
package test; public class Loger {
public void beforGetName(Int_S s) {
System.out.println("Before get user name"+s.getUsername());
} public void afterGetName(Int_S s) {
System.out.println("After get user name"+s.getUsername());
}
}
----------------------------
package test; public class SImpl implements Int_S { private String username="hxf"; @Override
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} public String getN() {
// TODO Auto-generated method stub
return username;
} }
---------------------------------
package test; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource; public class Test {
public static void main(String[] d){
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src/beanRefFactory.xml"));
//BeanFactory factory = new ClassPathXmlApplicationContext("beanRefFactory.xml");
SImpl greetingServices =(SImpl)factory.getBean("S");
System.out.println( greetingServices.getUsername());
}
} 期望Loger能自动system out,.但是没反应..console 只输出 asdf 我希望greetingServices.getUsername() 的时候 loger能自动输出
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货