2013년 8월 26일 월요일

Spring EL 사용하기

<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<util:properties id="mySettings" location="classpath:my.properties"/>
<context:component-scan base-package="com.my.classes" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="#{mySettings['jdbc.driver']}" />
<property name="url" value="#{mySettings['jdbc.url']}" />
<property name="username" value="#{mySettings['jdbc.user']}" />
<property name="password" value="#{mySettings['jdbc.password']}" />

</bean>
</beans>

자바에서 Spring EL을 사용해서 Annotation으로 기본 변수 값을 지정하려 하니 null 값이 반환되서 원인을 찾다보니 내가 사용할 클래스의 패키지를 스프링이 알게 해야 한다는것을 알았다.
진하게 표시된 부분을 통해서 Annotation을 찾을 수 있도록 해줬다.
java에서 사용 
  1. @Value("#{db['db.driverclassname']}")  
  2. private String defaultString;  


jsp 에서 사용 
  1. <spring:eval expression="@db['db.username']" />  


출처: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/expressions.html
http://stackoverflow.com/questions/12977484/null-pointer-when-calling-pre-instantiated-bean-from-spring-container-with-jsf
http://devkkaok.tistory.com/90
http://whiteship.tistory.com/2201
http://tazz009.tistory.com/507

댓글 없음:

댓글 쓰기