스프링 이클립스 기초 셋팅

박일한·2021년 12월 20일
0

java

목록 보기
1/1

BEAN factory 생성

public class BeanFactory implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
BeanFactory.context = context;
}
public static Object getBean(String sBeanName) {
return BeanFactory.context.getBean(sBeanName);
}
}

Tiles에 따른 jsp 매핑

@Controller
public class JspController {
@RequestMapping(value = "/*/.do", method = RequestMethod.GET)
public ModelAndView pcGet(HttpServletRequest req, HttpServletResponse res) {
String uri = req.getRequestURI();
ModelAndView mav = new ModelAndView();
mav.setViewName(uri);
req.setAttribute("PARAM_INFO", JsonUtil.parseToString(req.getParameterMap()));
return mav;
}
}

application-context

기본적으로 악용이지만 찍어내기 좋은 방법은
< bean class="com.test.mes.common.bean.BeanFactory" />
<context:property-placeholder location="classpath*:context.properties" />
여기까지는 properties 셋팅 가져오는 구분이고
<context:component-scan base-package="com.test.mes">
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
밑에 component-scan을 하면 전체 어노테이션을 알아서 다 땡겨와서
bean을 생성시킨다.

DB 설정

< bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
< property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
< property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521/orcl" />
< property name="username" value="test" />
< property name="password" value="1234" />
< property name="defaultAutoCommit" value="true" />
< /bean>
트랜잭션 설정까지
< bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
< property name="dataSource" ref="dataSource" />
< /bean>
< bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
< property name="dataSource" ref="dataSource" />
< property name="configLocation" value="classpath:sql/mybatis-config.xml"/>
< property name="mapperLocations" value="classpath:sql/com/test/mes/**/.xml" />
< property name="typeAliasesPackage" value="com.test.mes"/>
< /bean>
< bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
< constructor-arg index="0" ref="sqlSessionFactory" />
< /bean>
< bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
< property name="dataSource" ref="dataSource" />
< /bean>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get" propagation="REQUIRED" rollback-for="Exception" read-only="true" />
<tx:method name="sel
" propagation="REQUIRED" rollback-for="Exception" read-only="true" />
<tx:method name="ins" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="upd
" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="del" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="prcs
" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice>

< aop:config>
    < aop:pointcut id="requiredTx" expression="execution(* com.test.mes..*Service.*(..))" />
    < aop:advisor  id="txAdvisor" advice-ref="txAdvice" pointcut-ref="requiredTx" />
< /aop:config>

tiles 셋팅

< mvc:default-servlet-handler/>
< bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
< property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
< property name="order" value="1" />
< /bean>
< bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
< property name="definitions">
< list>
< value>/WEB-INF/tiles.xml< /value>
< /list>
< /property>
< /bean>
< bean id="reportResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
< property name="basename" value="views" />
< property name="order" value="0" />
< /bean>
< bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
< property name="defaultLocale" value="ko_KR" />
< /bean>
< bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

나의 생각

흠... 회사마다 개인마다 셋팅이 다르고 bean이나 component scan을 안하고
일일이 java에서 @Configuration @Bean을 통해서 등록하는 방법도 있다
보통 좀 규모가 있고 생각이 좀 큰 회사는 거의 configuration, bean을 통하여
많이 관리한다. 왜냐면 뭐를 bean으로 만들어서 사용하는지 관리가 되기 때문이다
나도 거의 si에서 유사한 플랫폼으로 찍어만 내다 보니까 좀 더 깊게 봐야할것 같다.

profile
긍정적인 삶을 갖자~~

0개의 댓글