How to create the Basic Application Using the Annotation.
In this Example we inject the dependencies of Foo object in the Bar class using the Spring Container and provide the wiring between the Spring Container & bean using the Annotation.
Foo.java
package com.piyushr.spring;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Foo{
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
this.name
}
@PostConstruct
public void init() {
System.out.println("init method called");
}
@PreDestory
public void cleanUp() {
System.out.println("cleanUp method called");
}
}
Here @PostConstruct annotation used to tell the Spring Container this method called before the instatiated the bean & the
@PreDestory annotation used to indicate this method called after the bean destory. So this method used for writing your
logic before and after bean instatiated and destory.
Now we see the AppConfig.java that class provide the replacement of context.xml or beans.xml
which used in XML Configuration.
AppConfig.java
package com.piyushr.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name="foo",initMethod="init",destroyMethod="cleanUp")
public Foo foo() {
Foo foo = new Foo();
foo.setName("Piyush");
return foo;
}
@Bean(name="secondaryFoo",initMethod="init",destroyMethod="cleanUp")
public Foo anotherFoo() {
Foo foo = new Foo();
foo.setName("Ramani");
return foo;
}
@Bean(name="bar")
public Bar bar() {
return new Bar();
}
}
Bar.java
package com.piyushr.spring;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Bar {
@Autowired
@Qualifier(value="secondaryFoo")
private Foo foo;
@Resource(name="foo")
private Foo foo2;
//@Required
public void setFoo(Foo foo) {
this.foo = foo;
}
public void printFooName(){
System.out.println(foo.getName());
System.out.println(foo2.getName());
}
}
Now We see the Test Class use for the test our application.
package com.piyushr.spring;
import java.util.Arrays;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestFooBar {
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
Bar bar = applicationContext.getBean("bar", Bar.class);
System.out.println(Arrays.toString(applicationContext.getBeanDefinitionNames()));
bar.printFooName();
applicationContext.registerShutdownHook();
}
}
output:
init method called
init method called
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, appConfig, bar, foo, secondaryFoo]
Ramani
Piyush
bean clean up called
bean clean up called
In this Example we inject the dependencies of Foo object in the Bar class using the Spring Container and provide the wiring between the Spring Container & bean using the Annotation.
Foo.java
package com.piyushr.spring;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Foo{
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
this.name
}
@PostConstruct
public void init() {
System.out.println("init method called");
}
@PreDestory
public void cleanUp() {
System.out.println("cleanUp method called");
}
}
Here @PostConstruct annotation used to tell the Spring Container this method called before the instatiated the bean & the
@PreDestory annotation used to indicate this method called after the bean destory. So this method used for writing your
logic before and after bean instatiated and destory.
Now we see the AppConfig.java that class provide the replacement of context.xml or beans.xml
which used in XML Configuration.
AppConfig.java
package com.piyushr.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name="foo",initMethod="init",destroyMethod="cleanUp")
public Foo foo() {
Foo foo = new Foo();
foo.setName("Piyush");
return foo;
}
@Bean(name="secondaryFoo",initMethod="init",destroyMethod="cleanUp")
public Foo anotherFoo() {
Foo foo = new Foo();
foo.setName("Ramani");
return foo;
}
@Bean(name="bar")
public Bar bar() {
return new Bar();
}
}
Bar.java
package com.piyushr.spring;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Bar {
@Autowired
@Qualifier(value="secondaryFoo")
private Foo foo;
@Resource(name="foo")
private Foo foo2;
//@Required
public void setFoo(Foo foo) {
this.foo = foo;
}
public void printFooName(){
System.out.println(foo.getName());
System.out.println(foo2.getName());
}
}
Now We see the Test Class use for the test our application.
package com.piyushr.spring;
import java.util.Arrays;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestFooBar {
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
Bar bar = applicationContext.getBean("bar", Bar.class);
System.out.println(Arrays.toString(applicationContext.getBeanDefinitionNames()));
bar.printFooName();
applicationContext.registerShutdownHook();
}
}
output:
init method called
init method called
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, appConfig, bar, foo, secondaryFoo]
Ramani
Piyush
bean clean up called
bean clean up called
No comments:
Post a Comment