Serach By Base Package : XML Bean « Spring « Java






Serach By Base Package

       
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: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">

    <context:component-scan base-package="bean"/>

</beans>


File: Main.java

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

public class Main {
  public static void main(String[] args) throws Exception {
    ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml");
    System.out.println(ac.getBean("simplestBean"));
  }
}

File: Magic.java

package bean;


public @interface Magic {
}


File: MarkedBean.java

package bean;


public class MarkedBean implements ComponentMarker {

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


File: SimpleBean.java

package bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * @author janm
 */
@Magic
class SimpleBean {
    private Dependency dependency;

    @Autowired(required = true)
    public void setDependency(Dependency dependency) {
        this.dependency = dependency;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("SimpleBean");
        sb.append(ObjectUtils.identityToString(this));
        sb.append("{dependency=").append(dependency);
        sb.append('}');
        return sb.toString();
    }
}
@Component
class Dependency {

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Dependency");
        sb.append(ObjectUtils.identityToString(this));
        sb.append("{}");
        return sb.toString();
    }
}


File: SimplestBean.java

package bean;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author janm
 */
@Component("simplestBean")
@Scope("prototype")
public class SimplestBean {

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


File: ComponentMarker.java

package bean;


public interface ComponentMarker {
}





           
       








Spring-SerachByBasePackage.zip( 4,454 k)

Related examples in the same category

1.XML Bean Injection
2.Reference another bean and set property
3.Static Factory
4.throw RequiredPropertyNotSetException
5.Properties File Based Spring Bean
6.Non Static Factory
7.Local Reference
8.Link With DataSource
9.Inheritance Demo
10.HierarchicalBeanFactory Demo
11.Filtered By Annotation
12.destroy method
13.dependency check Demo
14.Custom InitializationMethod
15.component scan
16.Component Scan and scope
17.Component Filter Assignable
18.implements BeanNameAware
19.Bean Lifecycle Initializing
20.Bean Lifecycle DisposableBean
21.Autowiring
22.Alias Bean Demo