public class Singleton {
private static Singleton self = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return self;
}
}
// 이후 생성자 호출 시
Singleton singleton = Singleton.getInstance();
// 객체(인스턴스)를 새로 생성하지 않음
<beans>
<bean id="식별자명" class="빈으로 등록할 클래스 경로">
<property name="값을 set할 변수" value="값" />
</bean>
</beans>
<beans>
<bean id="student1" class="com.example.Student">
<property name="name" value="홍길동" />
</bean>
</beans>
Student student1 = new Student();
student1.setName("홍길동");
<beans>
<bean id="식별자명1" class="빈으로 등록할 클래스 경로1">
<bean id="식별자명2" class="빈을 등록할 클래스 경로2">
<property name="값을 set할 변수" ref="식별자명1" />
</bean>
</beans>
<beans>
<bean id="maleStudent" class="com.example.maleStudent">
<bean id="student1" class="com.example.Student">
<property name="name" ref="maleStudent">
</bean>
</beans>
Student student1 = new Student();
student1.setName(maleStudent());
<beans>
<bean id="식별자명" class="클래스경로">
<constructor-arg value="초기 값" ref="빈 아이디"/>
</bean>
</beans>
1. Student.java
public class Student{
private String name;
private int grade;
private int class;
public Student(String name, int grade, int class){
this.name = name;
this.grade = grade;
this.class = class;
}
}
2.xml
<bean id="student1" class="com.example.Student">
<constructor-arg value="조진웅" />
<constructor-arg value="3" />
<constructor-arg value="10" />
</bean>
@ComponentScan(basePackages = "z.x.y")
@ComponentScan(basePackageClasses = ApplicationConfig.class)
public class ApplicationConfig {}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig {
@Bean
public Test test() {
return new Test();
}
@Bean
public Example example() {
Example example = new Example();
example.setTest(test());
return example;
}
}
public class HelloService{
// 주입받는 객체를 담는 필드
private Greeting greeting;
// 주입받기 위한 setter 메서드
public void setGreeting(Greeting greeting){
this.greeting = greeting;
}
public void sayHello(){
greeting.hi();
}
public class GreetingImpl extends Greeting{
public void hi(){
System.out.println("Hello");
}
}
}
<bean id="greetImpl" class="x.y.z.GreetingImpl"/>
<bean id="service" class="x.y.z.HelloService">
// name: setter 메서드 이름
// ref: 조립한 빈 이름
<property name="greeting" ref="greetImpl"/>
</bean>
public class Connector {
private String ip;
private int port;
private String username;
private String password;
public Connector(String username, String password) {
this.ip = "localhost";
this.port = 1521;
this.username = username;
this.password = password
}
public Connector(String ip, int port, String username, String password) {
this.ip = ip;
this.port = port;
this.username = username;
this.password = password
}
}
<bean class="x.y.z.Connector">
<constructor-arg name="username" value="hong"/>
<constructor-arg name="password" value="zxcv1234"/>
</bean>
<bean class="x.y.z.Connector">
<constructor-arg name="ip" value="192.168.10.65"/>
<constructor-arg name="port" value="2000"/>
<constructor-arg name="username" value="kim"/>
<constructor-arg name="password" value="1234zxcv"/>
</bean>