Annotation Component : XML Bean « Spring « Java Tutorial






File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy />

    <context:component-scan base-package="bean">
        <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
    </context:component-scan>


</beans>

File: Main.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import bean.Helper;
import bean.HelperDemo;

public class Main {
  public static void main(String[] args) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext(
        "context.xml");
    HelperDemo hd1 = (HelperDemo) ac.getBean("helperDemo");
    hd1.someOperation();
    System.out.println(hd1);
    HelperDemo hd2 = (HelperDemo) ac.getBean("helperDemo");
    hd2.someOperation();
    System.out.println(hd2);
    Helper helper = (Helper) ac.getBean("helper");
    System.out.println(helper);
  }
}

File: Helper.java

package bean;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("helper")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class Helper {
    private int count;

    public void work() {
        this.count++;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Helper");
        sb.append("{count=").append(count);
        sb.append('}');
        return sb.toString();
    }
}

File: HelperDemo.java

package bean;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class HelperDemo {
    @Autowired
    private Helper helper;

    public void setHelper(Helper helper) {
        this.helper = helper;
    }

    public void someOperation() {
        this.helper.work();
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("HelperDemo");
        sb.append("{helper=").append(helper);
        sb.append('}');
        return sb.toString();
    }
}
  Download:  Spring-AnnotationComponent.zip( 4,452 k)








28.4.XML Bean
28.4.1.XML Bean Injection with namespace
28.4.2.BeanName Aware
28.4.3.XML Based Bean Configuration
28.4.4.Required Property Not Set Exception
28.4.5.Method Loopup
28.4.6.Local Reference
28.4.7.Inheritance Demo
28.4.8.Get Method By Name
28.4.9.Factory Object Integration
28.4.10.Spring factory method
28.4.11.Factory Bean Demo
28.4.12.Autowiring
28.4.13.Annotation Component
28.4.14.Annotated Autowiring
28.4.15.Alias Bean Demo
28.4.16.After Returning Advice Demo