본문 바로가기

spring

@Scope - @Component 옵션 만약 은행업무에서 하나의 객체만을 가지고 실행되는 싱글톤패턴을 사용한다면 모든 사람이 하나의 객체만 가지고 은행업무를 보게되면 여러명의 사람이 하나의 계좌를 사용하는 꼴이 될 것이다. 그렇기 때문에 싱글톤을 사용하지 않는 방법으로 로직을 구현해야하는데 이때 @Scope를 사용하면 싱글톤을 사용하지 않을 수 있다. 스프링의 @Component의 scope를 설정하기 위한 어노테이션으로 명시하지 않을때 설정되는 scope의 기본값은 singleton 이다. 싱글톤을 하지 않기 위해서는 @Component 어노테이션 아래 @Scope("prototype") 으로 명시해 주면된다. 더보기
@Value, Spring EL @Value변수에 값을 초기화하기 위해 사용한다. @Value("#{dataInfo.name}") // Spring EL : #{표현식}, 만들어진 Component 객체를 이용,private는 getter를 이용해서 가져옴private String name; datainfo객체의 name 변수에 접근한 것. datainfo는 스프링이 만들어둔 객체.(설정파일에서든지 @Component 어노테이션에서든지 만든 객체) 기본적으로 @Value에 들어가는 값의 type은 String 이다. 하지만 변수 타입에 맞춰져서 알맞게 들어간다. @Value("30")private int age;@Value("1,2,3,4")//배열 처리private int arr[]; 메서드 안의 인자 안에서도 사용 가능하다. 더보기
namespace를 이용한 constructor injection과 property injection 설정 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> scope 속성  prototype : 객체 호출 시 매번 다른 객체 생성 singleton : 객체 호출 시 하나만 생성해서 돌려씀(default) --> index 속성 인자의 순서를 지정(0.. 더보기
property injection 속성 주입 만들어 놓은 변수에 기본적으로 값을 주는데 해당 변수의 setter를 이용하여 주입한다. 더보기
constructor injection 생성자 주입 스프링은 자바소스에서 객체를 생성하지 않고 스프링 컨테이너에서 객체를 만든 후 자바소스에서 스프링이 만들어 놓은 객체를 가져다 쓰기만 하면된다. 위의 예제는 객체를 만들때 생성자가 기본생성자가 아닌 인자가 있는 생성자의 객체를 생성할때 어떻게 하는가를 보여주는 예제이다. 더보기
@Resource @Resource는 @Autowired와 같이 property injection을 위한 어노테이션이다. 차이점은 @Autowired는 객체 타입을 보고 맵핑 된다면 @Resource는 객체 name(id)를 보고 맵핑이 된다. @Resource() 괄호에 객체 name을 명시하지 않으면 @Autowired 처럼 해당 레퍼런스변수의 타입과 똑같은 객체 name을 찾는다. @Resource(name="testClass") 라고 별도로 명시 한다면 해당 name을 가진 class와 맵핑한다. 다시 말해서 설정파일의 bean태그에 설정된 id(name도 가능)를 찾아서 맵핑 혹은 @Componentpublic class TestClass{} @Component("testClass")public class Tes.. 더보기
AbstractApplicationContext 스프링 설정파일 여러개 사용하기 String[] metas = new String[]{"anno1.xml","classpath:anno2.xml","classpath:anno3.xml","classpath:pack/anno3-1.xml"};AbstractApplicationContext context = new ClassPathXmlApplicationContext(metas); classpath라는 단어는 생략가능하다 경로만 신경써서 작성해주면된다. 더보기
어노 테이션을 사용하기 위한 설정 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">    context:component-scan base-pac.. 더보기