file applicationContext.xml
<bean id="employee" class="com.my.Employee">
</bean>
<bean id="observer" class="com.my.Observer" />
Employee.java
public Employee() {
System.out.println("Employee constructor executed");
}
private String name;
private String company;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
Observer.java
@Before("execution(* com.my.*.get*()) ")
public void beforeAdvice(JoinPoint jp) {
Employee emp = (Employee) jp.getTarget();
if("admin".equals(emp.getName())){
// access method get*()
}
else {
// can't access method get*()
}
}
AOPTest.java
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Employee employee = (Employee) context.getBean("employee");
employee.setName("admin");
employee.setCompany("IBM");
System.out.println("1 = "+employee.getName());
System.out.println("2 = " + employee.getCompany());
}
如果我改变
employee.setName("user");
//无法访问方法get *()。但方法getName()和getCompany()仍在运行。 我怎么能控制这个。如果setName(“user”)&gt;&gt;&gt;无法访问方法getName和getCompany()。 帮我 ! PLZ
答案 0 :(得分:1)
如何在Exception
中投放beforeAdvice
?
else {
throw new NotAuthorizedException("Can not access to method get");
}