Spring Component Include Exclude XML Project

Click here to download eclipse supported ZIP file



This is EmployeeDao.java file having the DAO logic to access the database information.


 

    
package com.cv.spring.annotation.component.dao;

import com.cv.spring.annotation.component.model.Employee;

/**
 @author Chandra Vardhan
 */
public interface EmployeeDao {
  public Employee createNewEmployee();
}


This is EmployeeDaoImpl.java file having the DAO logic to access the database information.


 

    
package com.cv.spring.annotation.component.dao;

import org.springframework.stereotype.Repository;

import com.cv.spring.annotation.component.model.Employee;

/**
 @author Chandra Vardhan
 */
@Repository
public class EmployeeDaoImpl implements EmployeeDao {
  public Employee createNewEmployee() {
    Employee e = new Employee();
    e.setId(1);
    e.setFirstName("Chandra");
    e.setLastName("Vardhan");
    return e;
  }
}


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


 

    
package com.cv.spring.annotation.component.model;

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

  private Integer id;
  private String firstName;
  private String lastName;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return "Employee [id=" + id + ", firstName=" + firstName
        ", lastName=" + lastName + "]";
  }
}


This is EmployeeService.java file having the service/business logic to call the DAO layer and get the information from database.


 

    
package com.cv.spring.annotation.component.service;

import com.cv.spring.annotation.component.model.Employee;

/**
 @author Chandra Vardhan
 */
public interface EmployeeService {
  public Employee createNewEmployee();
}


This is EmployeeServiceImpl.java file having the service/business logic to call the DAO layer and get the information from database.


 

    
package com.cv.spring.annotation.component.service;

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

import com.cv.spring.annotation.component.dao.EmployeeDao;
import com.cv.spring.annotation.component.model.Employee;

/**
 @author Chandra Vardhan
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
  
  @Autowired
  EmployeeDao dao;

  public Employee createNewEmployee() {
    return dao.createNewEmployee();
  }
}


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


 

    
package com.cv.spring.annotation.component;

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

import com.cv.spring.annotation.component.service.EmployeeService;

/**
 @author Chandra Vardhan
 */

public class TestSpringContext {
  
  private final static Logger LOGGER = Logger.getLogger(TestSpringContext.class);
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
        "spring.xml");

    // OR this will also work

    EmployeeService manager = (EmployeeServicecontext
        .getBean("employeeService");

    LOGGER.info(manager.createNewEmployee());
  }
}




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.annotation</groupId> <artifactId>SpringComponentIncludeExcludeXML</artifactId> <name>SpringComponentIncludeExcludeXML</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.1.6.RELEASE</org.springframework-version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> </dependencies> <build> <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> </build> </project>


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


 
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.cv.spring.annotation.component.service" />
<bean id="employeeService" class="com.cv.spring.annotation.component.service.EmployeeServiceImpl" />
<bean id="employeeDao" class="com.cv.spring.annotation.component.dao.EmployeeDaoImpl" />
</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