您好我正在尝试使用Spring aop @After annotation 更新数据库,但未调用使用@After
注释的方法。
这是我的SignupController:
package com.jpizza.web;
import com.jpizza.model.Customer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
@Controller
@RequestMapping("/signup")
public class SignupController {
@RequestMapping(method=GET)
public String signup(){
return "signup";
}
@RequestMapping(method=POST)
public String formHandler(Customer customer){
return "home";
}
}
这是我的Aspect类:
package com.jpizza.db;
import com.jpizza.model.Customer;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
/**
* Creed is a worker at jois pizza who registers users
*/
@Aspect
public class Creed implements CustomerDao{
@Autowired
private JdbcTemplate template;
//This method is not being executed
@After("execution(* com.jpizza.SignupController.formHandler(..)) && args(customer)")
@Override
public void saveCustomer(Customer customer) {
System.out.println("About to update data");
String sql = "INSERT INTO customer (username, email, password) VALUES (?,?,?)";
template.update(sql, customer.getUsername(), customer.getEmail(),customer.getPassword());
}
}
这是我的WebConfig.class:
@Configuration
@EnableWebMvc
@ComponentScan("com.jpizza.web")
@PropertySource("classpath:database.properties")
@EnableAspectJAutoProxy
public class WebConfig extends WebMvcConfigurerAdapter{
@Autowired
private Environment env;
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
dataSource.setUsername(env.getRequiredProperty("jdbc.username"));
dataSource.setPassword(env.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource source){
JdbcTemplate jt = new JdbcTemplate(source);
jt.setResultsMapCaseInsensitive(true);
return jt;
}
}
没有记录错误。我在saveCustomer(Customer)
执行后尝试执行formHandler(Customer)
但它无效。该页面正在被重定向home.jsp
有人可以告诉我我哪里出错吗?
修改
我按照建议改变了我的代码,但仍然没有执行@After
WebConfig.java:
@Configuration
@EnableWebMvc
@ComponentScan("com.jpizza.web")
@PropertySource("classpath:database.properties")
@EnableAspectJAutoProxy
public class WebConfig extends WebMvcConfigurerAdapter{
@Autowired
private Environment env;
//added creed as a bean
@Bean
public Creed creed(){
return new Creed();
}
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getRequiredProperty("jdbc.url"));
dataSource.setUsername(env.getRequiredProperty("jdbc.username"));
dataSource.setPassword(env.getRequiredProperty("jdbc.password"));
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource source){
JdbcTemplate jt = new JdbcTemplate(source);
jt.setResultsMapCaseInsensitive(true);
return jt;
}
}
Creed.java:
@Aspect
public class Creed implements CustomerDao{
@Autowired
private JdbcTemplate template;
//changed the package name
@After("execution(* com.jpizza.web.SignupController.formHandler(..)) && args(customer)")
@Override
public void saveCustomer(Customer customer) {
System.out.println("About to update data");
String sql = "INSERT INTO customer (username, email, password) VALUES (?,?,?)";
template.update(sql, customer.getUsername(), customer.getEmail(),customer.getPassword());
}
}
答案 0 :(得分:1)
您的配置存在两个问题:
答案 1 :(得分:0)
SignUpController类没有实现接口。尝试在@EnableAspectJAutoProxy
中启用proxyTargetClass@EnableAspectJAutoProxy(proxyTargetClass = true)