annotations

@Autowired and @Qualifier Annotations for Spring

We use @Autowired to automatically wire our bean dependencies. Example In the following example, we have autowired the home address for our student from the previous example. Spring will look for a single bean with a type that matches the type for home. If it finds this bean then it will automatically inject that bean …

@Autowired and @Qualifier Annotations for Spring Read More »

Using a Bean PostProcessor for Spring Annotations

I will cover the easy way to do this in a later post, but in this post we will look at how Spring processes annotations. Available JSR-250 Spring Annotations The spring-beans-3.x.jar contains the package org.springframework.beans.factory.annotation. In this package you will find the following BeanPostProcessors: AutowiredAnnotationBeanPostProcessor InitDestroyAnnotationBeanPostProcessor QualifierAnnotationBeanPostProcessor RequiredAnnotationBeanPostProcesor These PostProcessors facilitate the following annotations which …

Using a Bean PostProcessor for Spring Annotations Read More »

Spring Java Based Config

The following app-context.xml [sourcecode language=”xml”] <?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:context=”http://www.springframework.org/schema/context” xmlns:p=”http://www.springframework.org/schema/p” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd”> <context:annotation-config/> <bean id=”profile” class=”com.skills421.spring15.beans.Profile”/> <bean id=”student1″ class=”com.skills421.spring15.beans.Student” p:name=”John Doe” p:age=”21″/> <bean id=”student2″ class=”com.skills421.spring15.beans.Student” p:name=”Jane Doe” p:age=”19″/> </beans> [/sourcecode] can be replaced with the following SpringConfig.java [sourcecode language=”java”] package com.skills421.spring16.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.skills421.spring16.beans.Profile; import com.skills421.spring16.beans.Student; @Configuration …

Spring Java Based Config Read More »

Spring Annotations @Qualifier or @Resource

What Doesn’t Change app-context.xml [sourcecode language=”xml”] <?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:context=”http://www.springframework.org/schema/context” xmlns:p=”http://www.springframework.org/schema/p” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd”> <context:annotation-config/> <bean id=”profile” class=”com.skills421.spring15.beans.Profile”/> <bean id=”student1″ class=”com.skills421.spring15.beans.Student” p:name=”John Doe” p:age=”21″/> <bean id=”student2″ class=”com.skills421.spring15.beans.Student” p:name=”Jane Doe” p:age=”19″/> </beans> [/sourcecode] Student.java [sourcecode language=”java”] package com.skills421.spring15.beans; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Student { private Integer age; private String name; …

Spring Annotations @Qualifier or @Resource Read More »

Spring Annotations

[sourcecode language=”xml”] <?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:context=”http://www.springframework.org/schema/context” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd”> <context:annotation-config/> </beans> [/sourcecode] Annotations include: @Required @Autowired @Qualifier @Resource @PostConstruct @PreDestroy Autowired Annotations @AutoWired @AutoWired(required=false) @AutoWired @Qualifier(“student1”) @Resource(name=”student1″) AutoWired = required by default

Spring Bean Life Cycle

Initialization Callbacks Destruction Callbacks Initialization Callbacks public class Example1 implements InitializingBean { public void afterPropertiesSet() { } } <bean id=”exampleBean” class=”Example2″ init-method=”init”/> public class Example2 { public void init() { } } Destruction Callbacks public class Example1 implements DisposableBean { public void destroy() { } } <bean id=”exampleBean” destroy-method=”destroy”/> public class Example2 { public void …

Spring Bean Life Cycle Read More »