Spring Java Based Config

The following app-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">


<bean id="profile" class="com.skills421.spring15.beans.Profile"/>

<bean id="student1" class="com.skills421.spring15.beans.Student"
p:name="John Doe"

<bean id="student2" class="com.skills421.spring15.beans.Student"
p:name="Jane Doe"


can be replaced with the following SpringConfig.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;

public class SpringConfig
public Profile profile()
return new Profile();

public Student student1()
return new Student("James Davinson", 30);

public Student student2()
return new Student("Nicky Taylor", 19);

The MainApp.java code needs to be modified as follows:

AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);

Profile profile = (Profile) ctx.getBean("profile");


You will also need the CGLib jar

This can be added in the Pom as follows:


or downloaded from cglib.sourceforge.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Up ↑

%d bloggers like this: