반응형
스프링을 공부 한 내용을 정리합니다.
xml 설정 방식만하다가 java 설정도 해보았습니다.
Spring MVC로만 생성하다가 Spring Maven으로 프로젝트를 생성했습니다.
1. 프로젝트 생성
이클립스 - new - Spring Legacy project - Simple Spring Maven
2. Bean(DI) 학습
2-1. xml 설정
SimpleBean.java
package com.otrodevym.test;
public class SampleBean implements SampleBeanInterface{
private String message;
public SampleBean() {
message = "massage";
}
public SampleBean(String message) {
this.message = message;
}
@Override
public void setMessage(String message) {
this.message = message;
}
@Override
public String getMessage() {
return this.message;
}
@Override
public String toString() {
return "SampleBean [message=" + message + "]";
}
}
SampleBeanInterface.java
package com.otrodevym.test;
public interface SampleBeanInterface {
public String getMessage();
public void setMessage(String message);
}
BeanTest.xml
<bean id="bean1" class="com.otrodevym.test.SampleBean">
<property name="message" value="Hello, this is Bean Sample!" />
</bean>
App.java
package com.otrodevym.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String args[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
SampleBeanInterface sample = (SampleBeanInterface)ac.getBean("bean1");
System.out.println(sample);
}
}
2-2. 어노테이션 Bean 설정
SampleBeanConfig.java : xml 대체
package com.otrodevym.test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SampleBeanConfig {
@Bean
public SampleBeanInterface sampleBean() {
return sampleBean("this is sampleBean.");
}
}
App.java
package com.otrodevym.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String args[]) {
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext ac = new AnnotationConfigApplicationContext("bean.xml");
SampleBeanInterface sample = (SampleBeanInterface)ac.getBean("bean1");
System.out.println(sample);
}
}
2-3. 어노테이션 Component 설정
SampleBeanConfig.java
@Configuration
@ComponentScan
public class SampleBeanConfig {
@Bean
public SampleBean sampleBean() {
return new SampleBean("this is sampleBean.");
}
}
@ComponentScan 추가
BeanHolder.java
package com.otrodevym.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class BeanHolder {
@Autowired
private SampleBeanInterface sampleBean;
public void showMessage() {
System.out.println("sampleBean holder");
System.out.println(sampleBean);
}
}
BeanApp.java
package com.otrodevym.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BeanApp {
public static void main(String args[]) {
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// ApplicationContext ac = new AnnotationConfigApplicationContext(SampleBean.class);
// SampleBeanInterface sample = (SampleBeanInterface)ac.getBean("bean1");
// System.out.println(sample);
// ApplicationContext ac =
new AnnotationConfigApplicationContext(SampleBeanConfig.class);
// SampleBeanInterface bean =
(SampleBeanInterface)ac.getBean(SampleBeanInterface.class);
// System.out.println(bean);
ApplicationContext ac =
new AnnotationConfigApplicationContext(SampleBeanConfig.class);
BeanHolder holder = ac.getBean(BeanHolder.class);
holder.showMessage();
}
}
3. AOP 학습
3-1. xml 설정
POM.xml 추가
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-framework.version}</version>
</dependency>
AopBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="sampleAopBean"
class="com.otrodevym.test.aop.SampleAopBean">
<property name="message" value="this is Aop Bean"></property>
</bean>
</beans>
SampleAopBean.java
package com.otrodevym.test.aop;
public class SampleAopBean {
private String message;
public SampleAopBean() {
super();
}
public SampleAopBean(String message) {
super();
this.message = message;
}
public String getMessage() {
return this.message = message;
}
public void setMessage(String message) {
this.message = message;
}
public void printMaessag() {
System.out.println("message:[" + message +"]");
}
}
SampleMethodAdvice.java
package com.otrodevym.test.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
public class SampleMethodAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args,
Object target) throws Throwable {
System.out.println("after:" +method.getName() + "[" + target+ "]");
}
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("before: " + method.getName() + "[" + target + "]");
}
}
AopApp.java
package com.otrodevym.test.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopApp {
public static void main(String args[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
SampleAopBean bean1 = (SampleAopBean) ac.getBean("sampleAopBean");
bean1.printMaessag();
System.out.println("----------------------");
SampleAopBean bean2 = (SampleAopBean) ac.getBean("proxyFactoryBean");
bean2.printMaessag();
}
}
3-2. 어노테이션 설정
SampleAopConfig.java
package com.otrodevym.test.aop;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SampleAopConfig {
private SampleAopBean sampleAopBean = new SampleAopBean("this is message bean.");
private SampleMethodAdvice sampleMethodAdvice = new SampleMethodAdvice();
@Bean
SampleAopBean sampleAopBean() {
return sampleAopBean;
}
@Bean
SampleMethodAdvice samplemethodAdvice() {
return sampleMethodAdvice;
}
@Bean
ProxyFactoryBean proxyFactoryBean() {
ProxyFactoryBean bean = new ProxyFactoryBean();
bean.setTarget(sampleAopBean);
String[] names = new String[1];
names[0] = "sampleMethodAdvice";
bean.setInterceptorNames(names);
return bean;
}
}
AopApp.java
package com.otrodevym.test.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopApp {
public static void main(String args[]) {
ApplicationContext ac2 =
new AnnotationConfigApplicationContext(SampleAopConfig.class);
SampleAopBean bean3 = (SampleAopBean) ac.getBean("sampleAopBean");
bean1.printMaessag();
System.out.println("----------------------");
SampleAopBean bean4 = (SampleAopBean) ac.getBean("proxyFactoryBean");
bean2.printMaessag();
}
}
4. Aspect 학습
4-1. xml 설정
pom.xml
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
AopBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="sampleAspect"
class="com.otrodevym.test.aspect.SampleAspect"></bean>
<bean id="sampleAopBean"
class="com.otrodevym.test.aop.SampleAopBean">
<property name="message" value="this is Aop Bean"></property>
</bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
SampleAspect.java
package com.otrodevym.test.aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class SampleAspect {
@Before("execution(* com.otrodevym.test.aop.SampleAopBean.*(..))")
public void before() {
System.out.println("before");
}
@After("execution(* com.otrodevym.test.aop.SampleAopBean.*(..))")
public void after() {
System.out.println("after");
}
}
AspectApp.java
package com.otrodevym.test.aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.otrodevym.test.aop.SampleAopBean;
public class AspectApp {
public static void main(String args[]) {
ApplicationContext ac = new ClassPathXmlApplicationContext("aopbean.xml");
SampleAopBean sab = (SampleAopBean)ac.getBean("sampleAopBean");
String msg = sab.getMessage();
sab.setMessage(msg);
sab.printMaessag();
}
}
4-2. 어노테이션 설정
SampleAspectConfig.java
package com.otrodevym.test.aspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.otrodevym.test.aop.SampleAopBean;
@Configuration
@EnableAspectJAutoProxy
public class SampleAspectConfig {
@Bean
SampleAopBean sampleAopBean() {
return new SampleAopBean("this is aop Bean");
}
@Bean
public SampleAspect sampleAspect() {
return new SampleAspect();
}
}
AspectApp.java
package com.otrodevym.test.aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.otrodevym.test.aop.SampleAopBean;
public class AspectApp {
public static void main(String args[]) {
// ApplicationContext ac = new ClassPathXmlApplicationContext("aopbean.xml");
ApplicationContext ac =
new AnnotationConfigApplicationContext(SampleAspectConfig.class);
SampleAopBean sab = (SampleAopBean)ac.getBean("sampleAopBean");
String msg = sab.getMessage();
sab.setMessage(msg);
sab.printMaessag();
}
}
반응형
'개발(합니다) > Java&Spring' 카테고리의 다른 글
Spring maven project에서 tomcat 실행하기 (0) | 2018.12.18 |
---|---|
Spring maven project JPA 설정 정리 (0) | 2018.12.17 |
Spring 어노테이션 정리 (0) | 2018.12.15 |
Spring에서 데이터 주고 받기 (0) | 2018.12.13 |
[Spring] UserMapper 사용방법 (0) | 2018.12.12 |