Spring – Component | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • We use <context:component-scan base-package="" /> in xml file to scan declared Stereo types from packages.
  • We use <context:annotation-config /> in xml file to activates various annotations to be detected in bean classes: Spring’s @Required and @Autowired …

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create StereoType-Component project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    • commons-logging-X.X.jar
    • spring-aop-X.X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-expression-X.X.X.jar
  • * You can find namespace information from spring-beans-X.X.X.jar -> META-INF -> spring.schemas

spring.xml

<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-4.3.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<context:component-scan base-package="com.codeFactory.*" />
	<context:annotation-config />

	<bean class="com.codeFactory.beans.Engine">
		<property name="modelYear" value="2020" />
	</bean>
</beans>

Car.java

package com.codeFactory.beans;

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

/**
 * @author code.factory
 *
 */
@Component
public class Car {

	@Autowired(required=true)
	private Engine engine;

	public void print() {
		System.out.println("Engine - Model Year " + engine.getModelYear());
	}
}

Engine.java

package com.codeFactory.beans;

/**
 * @author code.factory
 *
 */
public class Engine {
	private int modelYear;

	public int getModelYear() {
		return modelYear;
	}

	public void setModelYear(int modelYear) {
		this.modelYear = modelYear;
	}
}

Client.java

package com.codeFactory.test;

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

import com.codeFactory.beans.Car;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		Car t = (Car) context.getBean(Car.class);
		t.print();
	}
}

Output:

Engine - Model Year 2020

. . . . .

  • Instead of @Component annotation we can also use @Named from J2EE.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create StereoType-J2EE project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    • commons-logging-X.X.jar
    • spring-aop-X.X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-expression-X.X.X.jar
    • javax.inject-1.jar
  • * You can find namespace information from spring-beans-X.X.X.jar -> META-INF -> spring.schemas

spring.xml

<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-4.3.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd">

	<context:component-scan base-package="com.codeFactory.*" />
	<context:annotation-config />

	<bean class="com.codeFactory.beans.Engine">
		<property name="modelYear" value="2020" />
	</bean>
</beans>

Car.java

package com.codeFactory.beans;

import javax.annotation.Resource;
import javax.inject.Named;

/**
 * @author code.factory
 *
 */
/* J2EE Named and Resource annotation */
@Named
public class Car {

	@Resource
	private Engine engine;

	public void print() {
		System.out.println("Engine - Model Year " + engine.getModelYear());
	}
}

Engine.java

package com.codeFactory.beans;

/**
 * @author code.factory
 *
 */
public class Engine {
	private int modelYear;

	public int getModelYear() {
		return modelYear;
	}

	public void setModelYear(int modelYear) {
		this.modelYear = modelYear;
	}
}

Client.java

package com.codeFactory.test;

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

import com.codeFactory.beans.Car;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		Car t = (Car) context.getBean(Car.class);
		t.print();
	}
}

Output:

Engine - Model Year 2020

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 )

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