Reputation: 1906
I'm trying to use the default aop proxy provided by Spring, but i'm getting an execption error.
Here is my code: A first class that run the test example.
@EnableAspectJAutoProxy()
@ComponentScan(basePackageClasses = {MyDaoRepository.class, MyService.class,MyAdvices.class})
@Configuration
public class SpringAdvices {
public static void main( String[] args ) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringAdvices.class);
service myService = ctx.getBean(MyService.class);
Person p1 = (Person) ctx.getBean("person");
myService.save(p1);
}
@Bean
public Person person(){
return Person.builder()
.name("Bagna")
.age(52)
.profession(null)
.dateOfBirth(LocalDate.of(1950,12,13))
.build();
}
}
A second class that represent my advices:
@Aspect
@Component
public class MyAdvices {
@Before("execution(boolean *.dao.save(..))")
public void beforesavamethod(){
System.out.println("beforesavamethod");
}
@After("execution(boolean *.dao.save(..))")
public void aftersavamethod(){
System.out.println("aftersavamethod");
}
}
My service and repository classes:
@Service
public class MyService implements service {
@Autowired
MyDaoRepository myDaoRepository;
@Override
public boolean save( Person person ){
return this.myDaoRepository.save(person);
}
@Override
public boolean delete(Person person){
return this.myDaoRepository.delete(person);
}
}
public interface service {
public boolean save( Person person );
public boolean delete( Person person );
}
@Repository
public class MyDaoRepository implements dao {
List<Person> personList = new ArrayList<>();
@Override
public boolean save( Person person ){
return this.personList.add(person);
}
@Override
public boolean delete( Person person ){
return this.personList.remove(person);
}
}
public interface dao {
public boolean save( Person person );
public boolean delete( Person person );
}
The exceptin I'm getting concern the injection of the dao object to the service object.
UnsatisfiedDependencyException: Error creating bean with name 'myService': Unsatisfied dependency expressed through field 'myDaoRepository'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myDaoRepository' is expected to be of type 'aop.question_005.dao.MyDaoRepository' but was actually of type 'com.sun.proxy.$Proxy22'
I can solve this problem by enabling the GCLIB mechanism, but i'm wondering how can i solve this problem using the same JDK dynamic proxy?
Upvotes: 1
Views: 800
Reputation: 9492
The problem in your code is that you are using the class MyDaoRepository instead of the dao
interface. You can cast your proxy to the interface dao but you can not cast it to its implementation. You need to modify your service code to use interfaces:
@Service
public class MyService implements service {
@Autowired
dao myDaoRepository;
@Override
public boolean save( Person person ){
return this.myDaoRepository.save(person);
}
@Override
public boolean delete(Person person){
return this.myDaoRepository.delete(person);
}
}
Upvotes: 3