<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ramsis</groupId>
<artifactId>springConfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>5.1.5.RELEASE</springframework.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</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>${springframework.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
</project>
---------------------------------------------------------------------------------------------------------------------
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan(basePackages="com.ramsis.bean")
public class Config {
}
package com.ramsis.bean;
import org.springframework.stereotype.Component;
@Component
public class Employee {
int id;
String name;
String department;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
------------------------------------------------------------------------------------------------------------------------
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.ramsis.bean.Employee;
import com.ramsis.config.Config;
public class Runner {
public static void main(String args[]) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Employee employee =context.getBean("employee",Employee.class);
employee.setName("Aman");
employee.setDepartment("IT Prod");
System.out.println("name: "+employee.getName());
System.out.println("Department: "+employee.getDepartment());
context.close();
}
}
No comments:
Post a Comment