Develop/Spring MVC

0817 Spring Start

포페PostFace 2022. 8. 22. 17:40


 Spring 다운 받는 방법

Eclipese -> 상단 메뉴 중 help -> 
Eclipse Marketplace -> 상단 메뉴 중search  ->
 "sts"로 검색(go) -> Spring Tools 3 (Standalone Edition) 3.9.14.RELEASE -> Install
-> 체크박스 해제하지 말고 Confirm(그대로 두면 됨, 이때 시간이 좀 가게됨.) ->
 I accept the terms of the license agreements 체크 -> Finish -> 
그러면 Eclipse 우측 하단에 Installing Software로 진행률 확인 가능함. -> 
select all, Trust어쩌구 -> restart Now -> 다시 Eclipse 실행됨 -> 
switchWorkspace -> Spring_~~ 생성(새로운 워크스페이스 생성) -> 
File -> new -> others -> Spring -> Spring Legacy Project -> 
Spring MVC Project 선택 후 next -> 
이때 최소 3개의 Package를 생성해야함 (example: "aa.bb.cc") -> 
src/main/java에 자동으로 HomeController 파일 생성 되어있음. -> 
프로젝트 우클릭 후 Run as -> 
Run on Server -> localhost에서 apache어쩌구 누르고 실행.

DI : dependency injection 의존 주입

생성자를 통한 주입,인터페이스를 통한 주입


갖가지 제어장치를 통해 메뉴얼을 설정해놓는듯이 설계해
컴퓨터 의존도를 낮춘다.

appContext.xml (Spring Bean)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 
		sample3.PointImpl point = new sample3.PointImpl();
		point.setXpos(3.0);
		point.setYpos(4.0);
	 -->
	<bean class="sample3.PointImpl" id="point">
		<property name="xpos">
			<value type="double">3.0</value>
		</property>
		<property name="ypos">
			<value type="double">4.0</value>
		</property>
	</bean>
	<!-- 
		CircleImpl circle=new CircleImpl(point, 10.0);
		circle.display(); -->
	<bean class="sample3.CircleImpl" id="circle">
		<constructor-arg>
			<ref bean="point"/>
		</constructor-arg>
		<constructor-arg>
			<value type="double">10.0</value>
		</constructor-arg>
	</bean>	
</beans>




Main에서 호출

Resource resource=new ClassPathResource("appContext.xml");
		
BeanFactory factory=new XmlBeanFactory(resource);
Point point = (Point)factory.getBean("point");
System.out.println(point.getXpos()+","+point.getYpos());
		
Circle circle=(Circle)factory.getBean("circle");
circle.display();





Spring Bean Configuration File 생성 후
c:를 통해 생성자 설정,
p:를 통해 property 설정하기
하단 Namespaces에서 c,p 체크후
하단의 코드처럼 작성한다.

<?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: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.xsd">
	
	
	<bean c:height="162.8" c:age="30" c:name="조이" id="per" class="myPkg2.PersonImpl"> 
	</bean>
<!-- 	
	<bean class="myPkg2.PersonImpl" id="per">
		<constructor-arg>
			<value>조이</value>
		</constructor-arg>
		<constructor-arg>
			<value type="int">30</value>
		</constructor-arg>
		<constructor-arg>
			<value type="double">168.2</value>
		</constructor-arg>			
	</bean> -->
	<bean class="myPkg2.StudentImpl" id="stu" p:kor="70" p:eng="80" p:per-ref="per">
	</bean>
	<!-- 
	<bean class="myPkg2.StudentImpl" id="stu">
		<property name="per">
			<ref bean="per"/>
		</property>
		<property name="kor">
			<value type="int">70</value>
		</property>
		<property name="eng">
			<value type="int">80</value>
		</property>
	</bean> -->
	<bean class="myPkg2.MyInfo" id="my" p:stu-ref="stu" p:per-ref="per"/>
	
	<!-- 
	<bean class="myPkg2.MyInfo" id="my">
		<property name="per">
			<ref bean="per"/>
		</property>
		<property name="stu">
			<ref bean="stu"/>
		</property>
	</bean>	 -->
</beans>




AOP(Aspect Oriented Programming)
단점 지양 프로그래밍
핵심로직과 공통로직을 분리해 공통 모듈을 프로그래밍 해
여러코드에 쉽게 적용할 수 있도록 해준다.

<dependency>
스프링 프로젝트 최하단에 pom.xml에는
<dependencies>라는 태그가 있다.
JSP의 jar파일들과 같은 역할을 하며
어떠한 것을 추가할때는 
<dependency>태그를 통해 설정해주면 된다.

AOP 사용시 추가해야하는 것

<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.6.11</version>
</dependency>



메이븐 중앙 저장소
https://mvnrepository.com 에서 검색해서 찾아도 된다.


AspectJ Pointcut Expression 예시
excution(public void set*(..)) : 리턴타입이 void고 메소드 이름이 set으로 시작하고, 파라메터가 0개 이상인 메소드 호출.
excution(* mypkg.core.*.*())  : 리턴타입 패키지.패키지.클래스.메소드순이다. 메소드옆 괄호가 비어있으므로 받는 데이터가 없어야한다.

advice: 공통기능을 언제 어떤걸 수행할지 정의한다.

<aop:config>
	<aop:aspect ref="mylogin"  order="1">
		<aop:before method="login" pointcut="execution(* example3.BoardImpl.*())"/>
	</aop:aspect>
</aop:config>



example3.BoardImpl의 매개변수 없는 아무 메소드 실행하기 전에 
mylogin으로 관리하는 example3.Login클래스 안에 login 메소드를 실행하라
order="1" 를 통해 순서를 지정할 수 있다.












'Develop > Spring MVC' 카테고리의 다른 글

0819 Spring Redirect AutoWired Valid  (0) 2022.08.22
0818 Spring  (0) 2022.08.22