Monday, February 12, 2018

Spring Setter and Constructor Injection Example

Setter Injection

Employee.java


/**
 * @author anthakur
 *
 */
public class Employee {
    private int id;
    private String name;
    private int salary;



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

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

applicationContext.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.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.bebo.*"></context:component-scan>
    <context:property-placeholder location="classpath:/application.properties"/>

    <bean id="employee" class="com.bebo.model.Employee">
        <property name="id" value="1">      </property> 
         <property name="name" value="Anil Thakur">      </property> 
        <property name="salary" value="100">      </property> 
       
      </bean>
</beans>


pom.xml

<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>SpringDemo2</groupId>
  <artifactId>SpringDemo2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
 <properties>
  <spring.version>4.0.2.RELEASE</spring.version>
  <servlet.api.version>2.5</servlet.api.version>
  <jsp.api.version>2.2</jsp.api.version>
  <jstl.version>1.2</jstl.version>
  <oracle.version>11.2.0.3</oracle.version>
 </properties>


 <dependencies>


  <!-- servlet dependency -->
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>${jsp.api.version}</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.1.1</version>
  </dependency>


  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
   <exclusions>
    <exclusion>
     <groupId>commons-logging</groupId>
     <artifactId>commons-logging</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${spring.version}</version>
  </dependency>


  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <!-- All Mail related stuff + Much more -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${spring.version}</version>
  </dependency>
 </dependencies>
</project>

MainClass.java

/**
 *
 */
package com.bebo;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.bebo.model.Employee;

/**
 * @author anthakur
 *
 */
public class MainClass {

    /**
     * @param args
     */
    public static void main(String[] args) {

       
         Resource resource =new ClassPathResource("applicationContext.xml");
         BeanFactory beanFactory =new XmlBeanFactory(resource);
         
         Employee employee =(Employee) beanFactory.getBean("employee");
         System.out.println(employee.toString());
        }
}

Constructor Injection

pom.xml will be same

MainClass.java will be same


In ApplicationContext.xml

    <bean id="employee" class="com.bebo.model.Employee">
        <constructor-arg value="1" ></constructor-arg>
         <constructor-arg name="name" value="Anil" ></constructor-arg>
          <constructor-arg value="100" ></constructor-arg>      

    </bean>


Output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Employee [id=1, name=Anil, salary=100]

No comments:

Post a Comment