Announcement Announcement Module
Collapse
No announcement yet.
AOP not applied Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP not applied

    I am creating a simple program to learn AOP, but AOP is not getting applied to my program.

    My configuration file is helloAop.xml:
    PHP Code:
    <?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
    [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
    [url]http://www.springframework.org/schema/aop[/url]
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
        <bean id="greetingService"
            class="com.springinaction.chapter01.helloAop.GreetingServiceImpl">
            <property name="greeting" value="Hello World!" />
        </bean>
        <bean id="aopbean" class="com.springinaction.chapter01.helloAop.AopBean" />
        <aop:config>
            <aop:aspect ref="aopbean">
                <aop:pointcut id="samplePointcut" expression="execution(* *.sayGreeting(..))" />
                <aop:before method="sampleBefore" pointcut-ref="samplePointcut" />
                <aop:after-returning method="sampleAfter"
                    pointcut-ref="samplePointcut" />
            </aop:aspect>
        </aop:config>
    </beans>
    GreetingServiceImpl Java file is:

    PHP Code:
    package com.springinaction.chapter01.helloAop;

    public class 
    GreetingServiceImpl implements GreetingService {
        private 
    String greeting;

        public 
    GreetingServiceImpl() {
        }

        public 
    GreetingServiceImpl(String greeting) {
            
    this.greeting greeting;
        }

        public 
    void sayGreeting() {
            
    System.out.println(greeting);
        }

        public 
    void setGreeting(String greeting) {
            
    this.greeting greeting;
        }

    My program to access bean from configuration file is HelloApp.java:

    PHP Code:
    package com.springinaction.chapter01.helloAop;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.FileSystemResource;

    public class 
    HelloApp {
        public static 
    void main(String[] argsthrows Exception {
            
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
                    
    "helloAop.xml"));
            
    GreetingService greetingService = (GreetingServicefactory
                    
    .getBean("greetingService");
            
    greetingService.sayGreeting();
        }

    When I am running HelloApp.java file I am getting below output:

    PHP Code:
    Feb 222013 3:37:22 PM org.springframework.core.CollectionFactory <clinit>
    INFOJDK 1.4collections available
    Feb 22
    2013 3:37:22 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO
    Loading XML bean definitions from file [D:\Chaitanya\Others\Learning\Workspaces\Spring\SpringExamples1\helloAop.xml]
    Hello World
    AOP is not getting applied to GreetingServiceImple bean, please help me in solving my issue.

  • #2
    My problem solved, I have to use ApplicationContext instead of BeanFactory.

    Comment

    Working...
    X