Why doesn't AspectJ compile-time weaving of Spring's @Configurable work?

后端 未结 4 1509
后悔当初
后悔当初 2020-12-02 13:51

Update 5: I\'ve downloaded the latest Spring ToolsSuite IDE based on the latest Eclipse. When I import my project as a Maven project, Eclipse/STS appears to

4条回答
  •  悲哀的现实
    2020-12-02 14:24

    making a field of a @configurable class Autowired throws NullPointerException if you do not configure your spring properly for this annotation. follow these steps to make @configurable annotations work properly

    This method is called AspectJ build time weaving to inject spring beans to your non-spring-made classes.

    First step is to install these plugins in eclipse:

    From these two update sites install whatever eclipse suggests:

    http://download.eclipse.org/tools/ajdt/43/update
    http://dist.springsource.org/release/AJDT/configurator/ 
    

    After installing,right-click on project and Do:

    Configure > Convert to Aspectj
    Maven > Update
    

    Next, you need to add these to your pom.xml:

    Under Dependencies Add:

    
        org.springframework
        spring-aspects
        4.0.2.RELEASE
    
    

    Under Plugins Add:

            
                org.codehaus.mojo
                aspectj-maven-plugin
                1.5
                
                    true
                    1.7
                    1.7
                    ignore
                    1.7
                    UTF-8
                    false
                    
                        
                            org.springframework
                            spring-aspects
                        
                    
                
                
                    
                        
                            compile
                            test-compile
                        
                    
                
                
                    
                        org.aspectj
                        aspectjrt
                        1.7.0
                    
                    
                        org.aspectj
                        aspectjtools
                        1.7.0
                    
                
            
    

    Important: DO NOT use any tag under tag. your pom.xml needs to be something like this:

    
        ....
        
             
                        ....
            
                    ....
        
        
            
                
                                ....
                
                            ....
            
        
    
    

    finally add to your spring application context config file.

    Now you can annotate a POJO class as @Configurable and inject spring beans in it using @Autowired annotation. this way whenever you make a new instance of that POJO it will be configured (e.g. injected with dependencies) automatically.

提交回复
热议问题