Skip to content

Commit

Permalink
Autowiring by Constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
mozammel committed Nov 21, 2014
1 parent a8a0b46 commit 14bc7cc
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 0 deletions.
22 changes: 22 additions & 0 deletions springtutorial21/.classpath
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
29 changes: 29 additions & 0 deletions springtutorial21/.project
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>springtutorial04</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
4 changes: 4 additions & 0 deletions springtutorial21/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
5 changes: 5 additions & 0 deletions springtutorial21/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
4 changes: 4 additions & 0 deletions springtutorial21/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
16 changes: 16 additions & 0 deletions springtutorial21/.springBeans
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[3.6.2.201410090854-RELEASE]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
<config>src/main/java/com/livingoncodes/spring/springtutorial/beans/beans.xml</config>
</configs>
<autoconfigs>
</autoconfigs>
<configSets>
</configSets>
</beansProjectDescription>
40 changes: 40 additions & 0 deletions springtutorial21/pom.xml
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.livingoncodes.spring</groupId>
<artifactId>springtutorial04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springtutorial04</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.11.RELEASE</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,26 @@
package com.livingoncodes.spring.springtutorial;

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

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{

ApplicationContext context = new ClassPathXmlApplicationContext("com/livingoncodes/spring/springtutorial/beans/beans.xml");

Logger logger = (Logger) context.getBean("logger");

logger.writeConsole("Hello there");
logger.writeFile("Hi there");


((ClassPathXmlApplicationContext) context).close();
}
}
@@ -0,0 +1,10 @@
package com.livingoncodes.spring.springtutorial;

public class ConsoleWriter implements LogWriter {

public void write(String text) {
System.out.println(text);

}

}
@@ -0,0 +1,10 @@
package com.livingoncodes.spring.springtutorial;

public class FileWriter implements LogWriter {

public void write(String text) {
System.out.println("Log to file: " + text);

}

}
@@ -0,0 +1,6 @@
package com.livingoncodes.spring.springtutorial;

public interface LogWriter {
public void write(String text);

}
@@ -0,0 +1,39 @@
package com.livingoncodes.spring.springtutorial;

public class Logger {

private LogWriter consoleWriter;
private LogWriter fileWriter;


public Logger(ConsoleWriter consoleWriter, FileWriter fileWriter) {
this.consoleWriter = consoleWriter;
this.fileWriter = fileWriter;
}

public LogWriter getConsoleWriter() {
return consoleWriter;
}

public void setConsoleWriter(LogWriter consoleWriter) {
this.consoleWriter = consoleWriter;
}

public LogWriter getFileWriter() {
return fileWriter;
}

public void setFileWriter(LogWriter fileWriter) {
this.fileWriter = fileWriter;
}

public void writeFile(String text) {
fileWriter.write(text);
}

public void writeConsole(String text) {
consoleWriter.write(text);

}

}
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">


<bean name="consoleWriter" class="com.livingoncodes.spring.springtutorial.ConsoleWriter">
</bean>


<bean id="fileWriter" class="com.livingoncodes.spring.springtutorial.FileWriter">
</bean>

<bean id="logger"
class="com.livingoncodes.spring.springtutorial.Logger"
autowire="constructor">
</bean>
</beans>
@@ -0,0 +1,38 @@
package com.livingoncodes.spring.springtutorial;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

0 comments on commit 14bc7cc

Please sign in to comment.