Spring4 Dependency Injection With Collections Project

Click here to download eclipse supported ZIP file



This is College.java file having the source code to execute business logic.


 

    
package com.cv.spring.scope;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 @author Chandra Vardhan
 */
public class College {
  
  private List<String> namesList;
  private Set<String> addressesSet;
  private Map<String, String> facultyMap;
  Properties departmentProp;

  public List<String> getNamesList() {
    return namesList;
  }

  public void setNamesList(List<String> namesList) {
    this.namesList = namesList;
  }

  public Set<String> getAddressesSet() {
    return addressesSet;
  }

  public void setAddressesSet(Set<String> addressesSet) {
    this.addressesSet = addressesSet;
  }

  public Map<String, String> getFacultyMap() {
    return facultyMap;
  }

  public void setFacultyMap(Map<String, String> facultyMap) {
    this.facultyMap = facultyMap;
  }

  public Properties getDepartmentProp() {
    return departmentProp;
  }

  public void setDepartmentProp(Properties departmentProp) {
    this.departmentProp = departmentProp;
  }

}


This is MainProgram.java file having the source code to execute business logic.


 

    
package com.cv.spring.scope;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 @author Chandra Vardhan
 */
public class MainProgram {

  private final static Logger LOGGER = Logger.getLogger(MainProgram.class);
  

  public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
        "beans.xml");

    College college = (Collegecontext
        .getBean("college");

    LOGGER.info("=========================================================");
    
    LOGGER.info("Names list : : "+college.getNamesList());
    LOGGER.info("Addresses set : : "+college.getAddressesSet());
    LOGGER.info("Faculty map : : "+college.getFacultyMap());
    LOGGER.info("Department prop : : "+college.getDepartmentProp());
    
    LOGGER.info("=========================================================");  
    context.close();
  }
}




This is pom.xml file having the entries of dependency jars and information to build the application .


	
<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">.0" encoding="UTF-8"?> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.spring.scope</groupId> <name>Spring4DependencyInjectionWithCollections</name> <packaging>jar</packaging> <version>1.0</version> <properties> <java-version>1.8</java-version> <log4j-version>1.2.16</log4j-version> <org.springframework-version>4.2.0.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </pluginManagement> </build> <artifactId>Spring4DependencyInjectionWithCollections</artifactId> </project>


This is beans.xml file having the spring configuration properties.


 
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<beans:bean id="college" class="com.cv.spring.scope.College">
<beans:property name="namesList">
<beans:list>
<beans:value>CHANDRA VARDHAN</beans:value>
<beans:value>SHRAVYA</beans:value>
<beans:value>NANDHIKA</beans:value>
</beans:list>
</beans:property>
<!-- results in a setAddressesSet(java.util.Set) call -->
<beans:property name="addressesSet">
<beans:set>
<beans:value>INDIA</beans:value>
<beans:value>USA</beans:value>
<beans:value>AUSTRALIA</beans:value>
</beans:set>
</beans:property>
<beans:property name="facultyMap">
<beans:map>
<beans:entry key="1" value="RAJA" />
<beans:entry key="2" value="CHANDRA" />
<beans:entry key="3" value="DURGA" />
</beans:map>
</beans:property>
<beans:property name="departmentProp">
<beans:props>
<beans:prop key="one">CSE</beans:prop>
<beans:prop key="two">ECE</beans:prop>
<beans:prop key="three">IT</beans:prop>
<beans:prop key="four">EEE</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
</beans:beans>



This is log4j.properties file having the entries for logging the information into the console/file.



#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

No comments:

Post a Comment