introduction to spring framework (presentation - 143 slides)

143
!" #$%

Upload: ammayi100

Post on 07-Apr-2015

408 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

������� ������������������ ������������������ ������������������ �����������

��������������������������������������������������������

���������������

� �!"��#�� �$ �%

Page 2: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

&'��'���&'��'���&'��'���&'��'���

Page 3: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

(��������)

*+�����������

*,���������'����������

– -��������

Page 4: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

��������!.� ��'��

*������+������������������������������������������� �� �

* �������/��������� ����������������������

* ���!������!���!����������� ��������'���

* ��������������������������.� ���0��������'�������������1

Page 5: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�����������2���

* &���� ���������������!.� ��������� ����2����!���

* ������������������2��������2�����������

* -���� ������������������2����2�����-3����������!��

* -���� �����������4�������������

* ,��������������2��+���������������� ��� �

* 3��'������ ����������2�������2������ ���

* 5��� �� �������������.����!�����4������2�����������������4�������� ���� ����������������4� ���������

Page 6: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�������2������

Page 7: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�����������-� ���� ����

*-� ���� ��������������������2��/�!��

*-��������� ��!���2���2���������

Page 8: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

� ������6

*����#2����������������!����� ����

Page 9: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

� �������

* ������������#�����������������#���4���!�2������

Page 10: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

� ������7

*8������������� �����

Page 11: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

� �������

*�+������������/�������3&+&�

Page 12: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�������! �������

*� �������

– ��������9�/��������#��#����+����:���������:�'�������9�!4�8���+������

– &�������� �����.� ����� ����!���4����7– - ����'����������6$�

*;�����'�������

– +<�����=>����– 8���+������

Page 13: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3���63���63���63���6�������!�� ��������!�� ��������!�� ��������!�� �

Page 14: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

,���,���,���,���

Page 15: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

��,?:������� 4���.� ����

*��'��������2�,������?:������� 4���.� ����

– ����������������������2������– ,����������.� ��������������� ���

* ������������������� �������

– ,��2�������������!���

Page 16: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� ���4

*@�����������!��� �������

*@����������2�������

– ������2�������� ������A��?��A ���A�������������������4��A����������A ������ �����������A������� ����������A������ ����������

Page 17: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

B����� ���4

*���� ���4������������

* �������2���������/���

<beans><bean id="exampleBean" class="eg.ExampleBean"/><bean id="anotherExample" class="eg.ExampleBeanTwo"/>

</beans>

Page 18: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

B����� ���4

*C�����/���

InputStream input = new FileInputStream("beans.xml");BeanFactory factory = new XmlBeanFactory(input);

ExampleBean eb =(ExampleBean)factory.getBean("exampleBean");

ExampleBeanTwo eb2 =(ExampleBeanTwo)factory.getBean("anotherExample");

ExampleBean eb =(ExampleBean)factory.getBean("exampleBean", ExampleBean.class);

Can throw NoSuchBeanDefinitionExceptionCan throw NoSuchBeanDefinitionException

Can throw BeanNotOfRequiredTypeExceptionCan throw BeanNotOfRequiredTypeException

Page 19: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� ���!������

*&�����!����4����!�����������������������

package eg;public class ExampleBean {

private AnotherBean beanOne;private YetAnotherBean beanTwo;

public void setBeanOne(AnotherBean b) { beanOne = b; }public void setBeanTwo(YetAnotherBean b) { beanTwo = b; }

}

<bean id="exampleBean" class="eg.ExampleBean"><property name="beanOne"><ref bean="anotherExampleBean"/></property><property name="beanTwo"><ref bean="yetAnotherBean"/></property>

</bean>

<bean id="anotherExampleBean" class="eg.AnotherBean"/><bean id="yetAnotherBean" class="eg.YetAnotherBean"/>

Page 20: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

��������������

* ��������!�������������

package eg;public class ExampleBean {

private String s;private int i;

public void setStringProperty(String s) { this.s = s; }public void setIntegerProperty(int i) { this.i = i; }

}

<bean id="exampleBean" class="eg.ExampleBean"><property name="stringProperty"><value>Hi!</value></property><property name="integerProperty"><value>1</value></property>

</bean>

Page 21: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3������4�������

*,��'���������������!.� ��

*��������.'$!���$3������4������

– ���D���01?���D���01�����-���/�01?���-���/�01

* �������+'

– �������4����,������:��!���������������������@������������������

* �������������

– ,�����������@� ����3�����������������-��4��C8@

*,�����������

– ,������������,����:����,����E�!���������������

Page 22: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

��������������4��������

*�/����

<property name="intProperty"><value>7</value></property>

<property name="doubleProperty"><value>0.25</value></property>

<property name="booleanProperty"><value>true</value></property>

<property name="colorProperty"><value>0,255,0</value></property>

java.awt.Color is initialized with RGB valuesjava.awt.Color is initialized with RGB values

Page 23: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

��������������4��������

*�/����

<property name="classProperty"><value>java.lang.Object</value>

</property>

<property name="fileProperty"><value>/home/ziba/file.txt</value>

</property>

<property name="localeProperty"><value>pt_BR</value>

</property>

<property name="urlProperty"><value>http://java.net</value>

</property>

<property name="stringArrayProperty"><value>foo,bar,baz</value>

</property>

Page 24: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

,������������4��������

*:����/���

* �������������/���– ������������������2��������4������������������'���

DateFormat fmt = new SimpleDateFormat("d/M/yyyy");CustomDateEditor dateEditor = new CustomDateEditor(fmt, false);beanFactory.registerCustomEditor(java.util.Date.class, dateEditor);

<property name="date"><value>19/2/2004</value></property>

StringTrimmerEditor trimmer = new StringTrimmerEditor(true);beanFactory.registerCustomEditor(java.lang.String.class, trimmer);

<property name="string1"><value> hello </value></property><property name="string2"><value></value></property>

<property name="string2"><null/></property>

Page 25: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

.'$����$3���������

*3�����������/���

<property name="propertiesProperty"><value>

foo=1bar=2baz=3

</value></property>

<property name="propertiesProperty"><props>

<prop key="foo">1</prop><prop key="bar">2</prop><prop key="baz">3</prop>

</props></property>

Page 26: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

.'$����$@���

*@�����/���

<property name="listProperty"><list>

<value>a list element</value><ref bean="otherBean"/><ref bean="anotherBean"/>

</list></property>

Page 27: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

.'$����$���

* �����/���

<property name="setProperty"><set>

<value>a set element</value><ref bean="otherBean"/><ref bean="anotherBean"/>

</set></property>

Page 28: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

.'$����$;�

*;���/���

<property name="mapProperty"><map>

<entry key="yup an entry"><value>just some string</value>

</entry><entry key="yup a ref">

<ref bean="otherBean"/></entry>

</map></property>

Page 29: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

,������ ����

*,������ �����/���public class ExampleBean {

private AnotherBean beanOne;private YetAnotherBean beanTwo;private int i;public ExampleBean(AnotherBean b1, YetAnotherBean b2, int i) {

this.beanOne = b1;this.beanTwo = b2;this.i = i;

}}

<bean id="exampleBean" class="eg.ExampleBean"><constructor-arg><ref bean="anotherExampleBean"/></constructor-arg><constructor-arg><ref bean="yetAnotherBean"/></constructor-arg><constructor-arg><value>1</value></constructor-arg>

</bean>

<bean id="anotherExampleBean" class="eg.AnotherBean"/><bean id="yetAnotherBean" class="eg.YetAnotherBean"/>

Page 30: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

������2� 4 ��

*����� ��!��������� �� !4�����2 ���4�!�2��������2��������

public class ExampleBean {public void init() {

// do some initialization work}

}

<bean id="exampleBean" class="eg.ExampleBean"init-method="init"/>

Page 31: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

������2� 4 ��

*����� ��!�� �����������������������4���

public class ExampleBean {public void cleanup() {

// do some destruction work}

}

<bean id="exampleBean" class="eg.ExampleBean"destroy-method="cleanup"/>

Page 32: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3������43� �������,��2������

*;����������������2������/������3����������2���

<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName">

<value>${jdbc.driverClassName}</value></property><property name="url"><value>${jdbc.url}</value></property><property name="username"><value>${jdbc.username}</value></property><property name="password"><value>${jdbc.password}</value></property>

</bean>

jdbc.driverClassName=org.hsqldb.jdbcDriverjdbc.url=jdbc:hsqldb:hsql://production:9002jdbc.username=sajdbc.password=root

jdbc.propertiesjdbc.properties

Page 33: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3������43� �������,��2������

*�������������� ��2������

InputStream input = new FileInputStream("beans.xml");XmlBeanFactory factory = new XmlBeanFactory(input);

Properties props = new Properties();props.load(new FileInputStream("jdbc.properties"));

PropertyPlaceholderConfigurer cfg =new PropertyPlaceholderConfigurer();

cfg.setProperties(props);cfg.postProcessBeanFactory(factory);

DataSource ds = (DataSource)factory.getBean("dataSource");

Page 34: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

;�������'������ ���4���

*�/������!��������������������������������

package eg;public class MySingleton {

private static MySingleton instance = new MySingleton();private MySingleton() {}public static MySingleton getInstance() {

return instance;}

}

<bean name="mySingleton" class="...beans.factory.config.MethodInvokingFactoryBean"><property name="staticMethod">

<value>eg.MySingleton.getInstance</value></property>

</bean>

A FactoryBean delegates the bean creation to another class

A FactoryBean delegates the bean creation to another class

Page 35: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

MySingleton singleton =(MySingleton)ctx.getBean("mySingleton");

FactoryBean factory =(FactoryBean)ctx.getBean("&mySingleton");

� ���4������2���� �

*F����������2���� ���������2 ���4������2

Return the factoryReturn the factory

Return the bean created by the factoryReturn the bean created by the factory

Page 36: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-�'� ���2������

* ����������?3�����4���

*-���������

– �4��4���4���4���4�� ��G������������������� ���2�� ����G�������4��

– �4��������� ��G�������!����������� ����� ���������4����02������#����������������1

*:������� 4� �� ����

* ���(�����

*������� ������?:�����!�����������2 ��

* ���� ���4-���?���E�-���������2 ��

Page 37: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-���� �����,����/�-���� �����,����/�-���� �����,����/�-���� �����,����/�

Page 38: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

(��������-���� ����,����/�)

*-�����������2��!������������ ��������� ��!�������!4���� ��������

*@� ������2�!�����2��������

*@�������2��������� ����/��

*=���� �� �� ����/��

* �6H������������� ��

*- ������������� ��

*�'�������������

Page 39: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-���� ����,����/�

*�/���������� ���4

*,���'��������� ����/�

*������������

– �����4���B�-���� ����,����/�– ,���3��B�-���� ����,����/�– B�(�!-���� ����,����/�

*�/���

ApplicationContext ctx =new FileSystemXmlApplicationContext("c:/beans.xml");

ExampleBean eb = (ExampleBean)ctx.getBean("exampleBean");

Page 40: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-���� ����,����/�

*-���� ����,����/�� ��!������2����4�2����

String[] ctxs = new String[]{"ctx1.xml", "ctx2.xml"};

ApplicationContext ctx = new FileSystemXmlApplicationContext(ctxs);

Page 41: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

=���� �� �� ����/��

*�2��!����������2��������� ����/����������� ����������������� ����/�

*,�������� ����/������� �4

ApplicationContext parent = new ClassPathXmlApplicationContext("ctx1.xml");

ApplicationContext ctx =new FileSystemXmlApplicationContext("ctx2.xml", parent);

Page 42: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

8����� ��

*-���� ����,����/������������������ ���� ����

*-���� ����,����/��������

– 8����� �����8����� �0��������� ����1A2���4�G���2����C8@����$�$�Ifile:C:/test.datJA�����'��2�����������$�$�IWEB-INF/test.datJA �������������#C8@����$�$�Iclasspath:test.datJ

interface Resource {boolean exists();boolean isOpen();String getDescription();File getFile() throws IOException;InputStream getInputStream() throws IOException;

}

Page 43: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

8����� �������

*�����#���3������4������

*,��!���������� ��2������8����� ����������������!�����2��������

*�/���<property name="resourceProperty">

<value>example/image.gif</value></property>

Page 44: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�6H�

*������������ ������2����� �����������

*-���� ����,����/�������

– ����������;������0������� �����&!.� �KL���������������2�����@� ����� 1

Delegated to a “messageSource” beanDelegated to a “messageSource” bean

Page 45: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�6H�

*-���� ����,����/����� ����2�������I��������� �J�!��

– ;������������;��������� �������2 �

*�/���

– ��2���������2����������� ��!���������� ������M�����������������

<bean id="messageSource" class="...ResourceBundleMessageSource"><property name="basenames">

<value>messages,errors</value></property>

</bean>

Search in classpath:messages_pt_BR.properties errors_pt_BR.propertiesmessages_pt.properties errors_pt.propertiesmessages.properties errors.properties

Search in classpath:messages_pt_BR.properties errors_pt_BR.propertiesmessages_pt.properties errors_pt.propertiesmessages.properties errors.properties

Page 46: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�'����

*�'�������������

– -���� ����,����/����������'�������� ������������

– �����������������-���� ����@������������� ��'���'����

– -���� ������ ���/�����-���� �����'���– �����#����'����A,����/�8�2�������'���A,����/�,������'���A8�G����=������'���

Page 47: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�'����

*@����������'����

* �����������'���

public class MyListenerBean implements ApplicationListener {public void onApplicationEvent(ApplicationEvent e) {

// process event}

}

public class ExampleBean implements ApplicationContextAware {ApplicationContext ctx;public void setApplicationContext(ApplicationContext ctx)

throws BeansException {this.ctx = ctx;

}

public void sendEvent() {ctx.publishEvent(new MyApplicationEvent(this));

}}

Page 48: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� ���43���3�� �����

*,��!���������� ��2�������������� ���4����!���������

– -���� ����� ����/��� �����#���� ������ ���43���3�� ������!�������������!�����2���������������4�����!�2�����4�������!�������� �����

* ������������ ������!������������������� ���43���3�� �����������2 �

Page 49: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� ���43���3�� �����

*�/���M������� ����������������� ����/�

public class MyPostProcessor implements BeanFactoryPostProcessor {

void postProcessBeanFactory(ConfigurableListableBeanFactory bf) {

DateFormat fmt = new SimpleDateFormat("d/M/yyyy");CustomDateEditor dateEditor =

new CustomDateEditor(fmt, false);

bf.registerCustomEditor(java.util.Date.class, dateEditor);}

}

<bean id="myPostProcessor" class="eg.MyPostProcessor"/>

Page 50: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

,����������,��2������

*���� ���43���3�� ����������������������������2��� ��'��������������������2� ������������4��������

<bean id="customEditorConfigurer" class="...CustomEditorConfigurer"><property name="customEditors">

<map><entry key="java.util.Date">

<bean class="...CustomDateEditor"><constructor-arg index="0">

<bean class="java.text.SimpleDateFormat"><constructor-arg><value>d/M/yyyy</value></constructor-arg>

</bean></constructor-arg><constructor-arg index="1"><value>false</value></constructor-arg>

</bean></entry>

</map></property>

</bean>

Page 51: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�4�� ������ ����� ����/��

*-���� ����� ����/�����������4���� ������������ ������2�����!4�����+�������'��

– (�!����� �����0.'/$���'���$���'���,����/�1M�����������'���������!����4���������������� ��� ����/��������������������������'���

– ���'���M�� �����'���� ���'�������������� ����� ����/�������'���2���������!����� ����� ����/�

– �+�M�������2�����B;@��� �������������+��+��2���

* ��������������������������������������!���������!���2 ���4

Page 52: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-&3-&3-&3-&3

Page 53: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-��� �#&��������3�������

*,���������&&3

*:� �����������2���� �� 0��� �� ����1

*;������ ������2� �� ������������������������� ��� ��������������!.� ��

*C����

– 3�������� �– ���� �����������– �� ����4– @������– :�!������

Page 54: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-&3� �� ����

*-��� �

– ;������ ������2�� �� ���

*+��������

– 3�����������������/� �������2�������

*-�'� �

– - ����������������� ����.��������

*3���� ��

– �����2�.������������� �24������������'� ���������2���

*������� ����

– -����������������2������������'����� ���

Page 55: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3���� ��

* �����2�.������������� �24������������'� ���������2���

public interface Pointcut {ClassFilter getClassFilter();MethodMatcher getMethodMatcher();

}

public interface ClassFilter {boolean matches(Class clazz);

}

public interface MethodMatcher {boolean matches(Method m, Class targetClass);boolean matches(Method m, Class targetClass, Object[] args);boolean isRuntime();

}

Restricts the pointcut to a given set of target classes

Restricts the pointcut to a given set of target classes

Static pointcuts don't use the method arguments

Static pointcuts don't use the method arguments

Page 56: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3���� ���������������

*8���/�

<bean id="gettersAndSettersPointcut" class="org.springframework.aop.support.RegexpMethodPointcut"><property name="patterns">

<list><value>.*\.get.*</value><value>.*\.set.*</value>

</list></property>

</bean>

Match a Perl5 regexp to a fully qualified method name

Match a Perl5 regexp to a fully qualified method name

Page 57: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-�'� �

*- ����������������� ����.��������

*�/���

public interface MethodInterceptor extends Interceptor {Object invoke(MethodInvocation invocation) throws Throwable;

}

public class DebugInterceptor implements MethodInterceptor {

public Object invoke(MethodInvocation invocation)throws Throwable {System.out.println(">> " + invocation); // beforeObject rval = invocation.proceed();System.out.println("<< Invocation returned"); // afterreturn rval;

}}

Spring implements an advice with an interceptor chain around the jointpoint

Spring implements an advice with an interceptor chain around the jointpoint

Page 58: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-�'� ���4���

*-�������'� �

– �������'������/���

* ��2�����'� �

* ��������'� �

*-2���������������'� �

*������� ������'� �

Page 59: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�������-�'�����

*3���� ��-�'�����N�3���� ���O�-�'� �

*� ��!����#����'� ��������'����

*�/���

<bean id="gettersAndSettersAdvisor"class="...aop.support.RegexpMethodPointcutAroundAdvisor"><property name="interceptor">

<ref local="interceptorBean"/></property><property name="patterns">

<list><value>.*\.get.*</value><value>.*\.set.*</value>

</list></property>

</bean>

Page 60: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3��/4� ���4

*(�����3��/4� ���4�4��������'�����'�����'�����'���� �!.� ��

– 5��� ����2��������� ��������'� ������������!��������

– ������������������ ������������/4��!.� �– ��������+'�:4�� �3��/4����,F@����A��� �����/4������2 ������ �����

* ,�������-&3����/����������� ��4ProxyFactory factory = new ProxyFactory(myBusinessInterfaceImpl);factory.addInterceptor(myMethodInterceptor);factory.addAdvisor(myAdvisor);

MyBusinessInterface b = (MyBusinessInterface)factory.getProxy();

Page 61: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3��/4� ���4���

*C��������������/����2�� !���

* ����!������!�����/���

<bean id="personTarget" class="eg.PersonImpl"><property name="name"><value>Tony</value></property><property name="age"><value>51</value></property>

</bean>

PersonImpl implements Person interfacePersonImpl implements Person interface

Page 62: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3��/4� ���4���

*��������� ������?�'�����

* �������/4

<bean id="myAdvisor" class="eg.MyAdvisor"><property name="someProperty"><value>Something</value></property>

</bean>

<bean id="debugInterceptor" class="...aop.interceptor.NopInterceptor"></bean>

<bean id="person" class="...aop.framework.ProxyFactoryBean"><property name="proxyInterfaces"><value>eg.Person</value></property>

<property name="target"><ref local="personTarget"/></property><property name="interceptorNames">

<list><value>myAdvisor</value><value>debugInterceptor</value>

</list></property>

</bean>

Page 63: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3��/4� ���4���

*C���������!��

– ,����������������������person !�����������2�personTarget

– ,��!�� ����������������� ����� ����/������������ �4

<bean id="personUser" class="com.mycompany.PersonUser"><property name="person"><ref local="person" /></property>

</bean>

Person person = (Person) factory.getBean("person");

Page 64: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3��/4� ���4���

*�2�4��������������/4�� ������������2��������2 �

– ���������������4����/4�����,������/4�����,������/4�����,������/4�����,��� �����������������2����/4�����2 �����/4�����2 �����/4�����2 �����/4�����2 ��

– 3��/4�������/��������������� ���A ������ ����!4�,F@��

<bean id="person" class="...aop.framework.ProxyFactoryBean"><property name="proxyTargetClass"><value>true</value></property>

<property name="target"><ref local="personTarget"/></property><property name="interceptorNames">

<list><value>myAdvisor</value><value>debugInterceptor</value>

</list></property>

</bean>

Page 65: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-���3��/4

*-����� ����/4� ������

– +������ ��������������– ���� ����!���������!������� ��4����/���

*E���������������3��/4� ���4����2���� ��������!��

Page 66: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���E�-���3��/4,�����

* ���� ���������!4�!�����

<bean id="employee1" class="eg.Employee">...</bean><bean id="employee2" class="eg.Employee">...</bean>

<bean id="myInterceptor" class="eg.DebugInterceptor"/>

<bean id="beanNameProxyCreator"class="...aop.framework.autoproxy.BeanNameAutoProxyCreator"><property name="beanNames"><value>employee*</value></property><property name="interceptorNames">

<list><value>myInterceptor</value>

</list></property>

</bean>

Page 67: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-�'����-���3��/4,�����

*-����� ��4���������'����� ��� ����/�����!���

– � ���'�������������� ���������'� �– �2������� ��������������!�����������!������� ������!4������'� �

*C��2���������4���������'� �� ����������4�����4�!���������!.� ��

*������!��������������#�'������!.� �

Page 68: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-�'����-���3��/4,�����

*�/���

<bean id="debugInterceptor" class="app.DebugInterceptor"/>

<bean id="getterDebugAdvisor"class="...aop.support.RegexpMethodPointcutAdvisor"><constructor-arg>

<ref bean="debugInterceptor"/></constructor-arg><property name="pattern"><value>.*\.get.*</value></property>

</bean>

<bean id="autoProxyCreator"class="...aop.framework.autoproxy.AdvisorAutoProxyCreator"><property name="proxyTargetClass"><value>true</value></property>

</bean>

This advisor applies debugInterceptor to all get methods of any classThis advisor applies debugInterceptor to all get methods of any class

Page 69: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-�'� ���-&3��������

*;����#���'���������/4���

* ��������� ��

– =�������!������������� ��A-����������������2�����/4����!������ ������������������ ����������������������2���� ��������

– 3����������������� ��A-�������2������� ������� ����������������������������'� ���������������2�����!.� ��������������

Page 70: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

;���������!����;���������!����;���������!����;���������!����

Page 71: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� �#��'�������

*�������������2�����!���� ������������ ����������������M������4�� ��������?���������

/*** Normal comments* @@org.springframework.transaction.interceptor.DefaultTransactionAttribute()*/

public class PetStoreImpl implements PetStoreFacade, OrderService {...

}

/*** Normal comments* @@org.springframework.transaction.interceptor.RuleBasedTransactionAttribute()* @@org.springframework.transaction.interceptor.RollbackRuleAttribute(Exception.class)* @@org.springframework.transaction.interceptor.NoRollbackRuleAttribute("ServletException")*/

public void echoException(Exception ex) throws Exception {....

}

Annotated classAnnotated class

Annotated methodAnnotated method

Page 72: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� �#��'�������

* ����������'������2 �����������������������– C����+����,�����-����!����A��������� ����������������!���� �������������

– +�8#6PQ�0+:R�6$Q1�������

*C���– (����-&3A-����!������������������� �24���� ��

– ;���� ����!������ ��2��������A������� ���������������

– D�������

Page 73: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

3����3����3����3������������������������������������������������������������������������

Page 74: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

����������

*���� ���4��-���� ����,����/�����-&3��������!����2�������

*�������������������������������������������������������������-3��

Page 75: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

;��;��;��;��

Page 76: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�����;��;�����

*,�������������

SimpleMailMessage msg = new SimpleMailMessage();

msg.setFrom("[email protected]");msg.setTo("[email protected]");msg.setCc(new String[] {"[email protected]", "[email protected]"});msg.setBcc(new String[] {"[email protected]", "[email protected]"});msg.setSubject("my subject");msg.setText("my text");

Page 77: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

;�����������

*:�2�������������������

* �����������������

<bean id="mailSender"class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host"><value>smtp.mail.org</value></property><property name="username"><value>joe</value></property><property name="password"><value>abc123</value></property>

</bean>

MailSender sender = (MailSender) ctx.getBean("mailSender");

sender.send(msg);

Page 78: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

� ��������� ��������� ��������� ��������

Page 79: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

� ��������

*�����#�����������2��

– +'�������A����A�������

– S��� �0����M??���$G��� � �������$���?1A� ��������A+�!:�����A��������

Page 80: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

� ��������������

*�������������������������

public class MyTask extends TimerTask {public void run() {

// do something}

}

<bean id="myTask"class="...scheduling.timer.ScheduledTimerTask"><property name="timerTask">

<bean class="eg.MyTask"/></property><property name="delay"><value>60000</value></property><property name="period"><value>1000</value></property>

</bean>

Java bean that wraps a scheduled java.util.TimerTask

Java bean that wraps a scheduled java.util.TimerTask

Page 81: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

����� ���4���

*,������������ �������

* �����������������!��� ����������

<bean id="scheduler"class="...scheduling.timer.TimerFactoryBean"><property name="scheduledTimerTasks">

<list><ref bean="myTask"/></list></property>

</bean>

Creates a java.util.Timer objectCreates a java.util.Timer object

Page 82: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+E:�+E:�+E:�+E:�

Page 83: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+���������

*C�����+���������

Properties p = new Properties();p.setProperty("java.naming.factory.initial",

"org.jnp.interfaces.NamingContextFactory");p.setProperty("java.naming.provider.url",

"jnp://localhost:1099");

JndiTemplate jndi = new JndiTemplate(p);

Properties env = jndi.getEnvironment();

try {jndi.bind("Something", something);Object o = jndi.lookup("Something");jndi.unbind("Something");

}catch(NamingException e) {

...}

Page 84: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+���&!.� �� ���4���

*C������!�����������2��������

<bean id="jndiTemplate"class="org.springframework.jndi.JndiTemplate"><constructor-arg>

<props><prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop><prop key="java.naming.provider.url">jnp://localhost:1099</prop>

</props></constructor-arg>

</bean>

<bean id="something"class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiTemplate"><ref bean="jndiTemplate"/></property><property name="jndiName"><value>Something</value></property>

</bean>

Object o = ctx.getBean("something");

A FactoryBean delegates the bean creation to another class

A FactoryBean delegates the bean creation to another class

Page 85: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+:�,+:�,+:�,+:�,

Page 86: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+:�,�!��� ����

*;���+:�,���������������������������������

*������������������� ������������������2������� ��

*������������� ����2�����/ ��������������

Page 87: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+�! ������

*�/� ������S@�G������������������������������������ ������ ���

*����������'���8��������������/�� ������2�����������������'����

*�/���

DataSource ds = DataSourceUtils.getDataSourceFromJndi("MyDS");JdbcTemplate jdbc = new JdbcTemplate(ds);

jdbc.execute("drop table TEMP");

jdbc.update("update EMPLOYEE set FIRSTNME=? where LASTNAME=?",new String[] {"JOE", "LEE"});

Page 88: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+�! ������

*S�������������� ��'����� ��������

int maxAge = jdbc.queryForInt("select max(AGE) from EMPLOYEE");

String name = (String)jdbc.queryForObject("select FIRSTNME from EMPLOYEE where LASTNAME='LEE'",String.class);

List employees = jdbc.queryForList("select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE");

Returns an ArrayList (one entry for each row) of HashMaps(one entry for each column using the column name as the key)

Returns an ArrayList (one entry for each row) of HashMaps(one entry for each column using the column name as the key)

Page 89: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+�! ������

*S�������������� ��! �������

final List employees = new LinkedList();

jdbc.query("select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE", new RowCallbackHandler() {

public void processRow(ResultSet rs) throws SQLException {

Employee e = new Employee();e.setEmpNo(rs.getString(1));e.setFirstName(rs.getString(2));e.setLastName(rs.getString(3));

employees.add(e);}

});employees list will be populated with Employee objectsemployees list will be populated with Employee objects

Page 90: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+�! ������

* ���������� ������

jdbc.call(new CallableStatementCreator() {public CallableStatement createCallableStatement(Connection conn)throws SQLException {return conn.prepareCall("my query");

}}, params);

Page 91: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

+�! ������

*�� ��������

BatchPreparedStatementSetter setter =new BatchPreparedStatementSetter() {

public void setValues(PreparedStatement ps, int i)throws SQLException {...

}public int getBatchSize() {

return ...;}

};

jdbc.batchUpdate("update ...", setter);

Page 92: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�G�S���4?�G�C������!.� ��

*�� �������G����������������������+'� �����

class EmployeeQuery extends MappingSqlQuery {

public EmployeeQuery(DataSource ds) {super(ds, "select EMPNO, FIRSTNME, LASTNAME from EMPLOYEE where EMPNO = ?");declareParameter(new SqlParameter(Types.CHAR));compile();

}

protected Object mapRow(ResultSet rs, int rownum) throws SQLException {Employee e = new Employee();e.setEmpNo(rs.getString("EMPNO"));e.setFirstName(rs.getString("FIRSTNME"));e.setLastName(rs.getString("LASTNAME"));return e;

}

public Employee findEmployee(String id) {return (Employee) findObject(id);

}}

Convenience method to do strong typingConvenience method to do strong typing

Map a result set row to a Java objectMap a result set row to a Java object

Page 93: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�G���� ����

*�� �������G�����������������������������

SqlFunction sf = new SqlFunction(dataSource,"select count(*) from mytable");

sf.compile();

int rows = sf.run();

Page 94: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�/ ��������������

*����������S@�/ ����������:�- ����/ ������������ �4

– F����� ��������2����'���:�?+:�,�������������0�G�������� �����������������/ �������1

*C����8������/ ��������0�� �� ���1

*��� ���������� �'���2������� �� ������� �����/ ������

try {// do work

}catch (OptimisticLockingFailureException ex) {

// I'm interested in this}

Page 95: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

:�!��� ���� �����

*:����� �C����

– ���,���� ����01�����:����� ����+���01– ����,���� �����2E� ����401

*:��'��;����:����� �

– 8������������ ���� ������'��4����– ���!���������������� �������������������

* ������,���� ����:����� �

– 8���������4��������� ���� ����– ���!���������������� �������������������

Page 96: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� �����;���������� �����;���������� �����;���������� �����;������

Page 97: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� �����

*F��!������ �����

– �����!4��������� ��������'����������+�-– !����4�������������������������� ������������ ��

*@� ������ �����

– ������ �#��� �2� M�2����/���������� �������� �����������+:�,� ���� ����

– ���������� ����������������� ������������ ��

– ��������������������!��+�-����� ����

*:�22��������������������

Page 98: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���������������

*C�������������������������2������!������� ������ �����

– :�22���������� ���������������������������22��������'��������

* ���� ������������ ��!�

– 3������ – :� ����'��0������+��,;�1

Page 99: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� �����!��� ����

*���� ���������!��� ����!4����������2 ��3��2������ ����;����

– ������� ����0���� ����:�2�������1– ���0���� ���������1�– ����! �0���� ���������1

* ���� ����:�2�������

– ��������������������������������#���4������

* ���� ���������

– ��E������ ����01– ���8���! �&��401– ��8���! �&��401

Page 100: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� ����������

*�����#������2������� ����������

– +����� ����;����– :����� ����� ����;����– =�!��������� ����;����– +������ ����;����

Page 101: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�/����

*:�2�������+����� ����;����

<bean id="dataSource" class="...jndi.JndiObjectFactoryBean"><property name="jndiName"><value>MyDS</value></property>

</bean>

<bean id="transactionManager" class="...transaction.jta.JtaTransactionManager"/>

Data sources must be configured in the app server as transactional resources

Data sources must be configured in the app server as transactional resources

Page 102: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�/����

*:�2�������:����� ����� ����;����

<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource">...

</bean>

<bean id="transactionManager"class="...jdbc.datasource.DataSourceTransactionManager"><property name="dataSource">

<ref local="dataSource"/></property>

</bean>

Page 103: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�/����

*:�2�������=�!��������� ����;����

<bean id="sessionFactory"class="...orm.hibernate.LocalSessionFactoryBean">...

</bean>

<bean id="transactionManager" class="...orm.hibernate.HibernateTransactionManager"><property name="sessionFactory">

<ref local="sessionFactory"/></property>

</bean>

To make Hibernate use JTA you don't need HibernateTransactionManager, just configure a JtaTransactionManager and give to sessionFactory data sources obtained from JNDI

To make Hibernate use JTA you don't need HibernateTransactionManager, just configure a JtaTransactionManager and give to sessionFactory data sources obtained from JNDI

Page 104: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� ����������

*3������ ����� �����������

*,���������� ����������

*�/� ������������ ����

PlatformTransactionManager transactionManager =(PlatformTransactionManager) ctx.getBean("myTransactionManager");

TransactionTemplate transaction =new TransactionTemplate(transactionManager);

transaction.execute(new TransactionCallbackWithoutResult() {public void doInTransactionWithoutResult(TransactionStatus s) {

updateOperation1();updateOperation2();

}});

Page 105: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� ����������

*-�����������������2����� ����������

– ���3�����������'���0���1– �����������@�'��0���1– ���8��&��40!�����1– ���������0���1

Page 106: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

-�'�����

*����������2����������� ���������

– ���.�����������2� ��2��������– E���������� ��������� ���

* ������� �������� ��������

– -���� ��������'��������+�-����� �����– ����#��������� ����������!� �������A�����+:�,����� �����A���������������� ��+�-���+&�;

Page 107: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

:� ����'������ �����

*E��������2����� ����������

*�����������������������-&3

* ����������+��,;�

– 5������ �24����� �����!��'�����0���� ���2���1�������������'�����������

Page 108: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

:� ����'������ �����

*:�22������2����+��,;�

– ,��!�������������4�3&+&– E�����������+�-�0�����������+:�,��+:&��=�!�����1– =���� ����'������! �������– ,������!������� ������!��'����– :���������������������������2����� ����� ����/��� ����������� ���

Page 109: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

���� ����-����!������� �

*:�2������������� �������������������������

* ���� ����-����!�����������������2���������2�2��– PROPAGATION_NAME,ISOLATION_NAME,readOnly,+Exception1,-Exception2

– -�9O9�!�2�������/ ������������!����������� ������������ ������������� �����'����2�������/ ����������������T��9#9��������4�������������! �

*�/���– PROPAGATION_MANDATORY,ISOLATION_DEFAULT,-CreateException,-DuplicateKeyException

Page 110: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

:� ����'������ �����

*:�2����������� ���������� �����

<bean id="txAttributes"class="...MatchAlwaysTransactionAttributeSource"><property name="transactionAttribute">

<value>PROPAGATION_REQUIRED</value></property>

</bean>

<bean id="txInterceptor"class="...transaction.interceptor.TransactionInterceptor"><property name="transactionManager">

<ref bean="myTransactionManager"/></property><property name="transactionAttributeSource">

<ref bean="txAttributes"/></property>

</bean>

MatchAlwaysTransactionAttributeSourceapplies the same attributes to all methods

MatchAlwaysTransactionAttributeSourceapplies the same attributes to all methods

Page 111: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

:� ����'������ �����

*-���������'������ ����-����!������� �

<bean id="txAttributes"class="...interceptor.NameMatchTransactionAttributeSource"><property name="properties">

<value>get*=PROPAGATION_REQUIRED,readOnlyfind*=PROPAGATION_REQUIRED,readOnlyload*=PROPAGATION_REQUIRED,readOnlystore*=PROPAGATION_REQUIRED

</value></property>

</bean> NameMatchTransactionAttributeSource applies specific attributes to methods that match to a pattern

NameMatchTransactionAttributeSource applies specific attributes to methods that match to a pattern

Page 112: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

:� ����'������ �����

*-������/4�2������� ������!���

<bean id="autoProxyCreator"class="...framework.autoproxy.BeanNameAutoProxyCreator"><property name="interceptorNames">

<value>txInterceptor</value></property><property name="beanNames">

<value>*Dao</value></property>

</bean>

Page 113: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

:� ����'������ �����

*C��������������!����<bean id="autoproxy"

class="...aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>

<bean id="txAdvisor"class="...transaction.interceptor.TransactionAttributeSourceAdvisor"autowire="constructor">

</bean>

<bean id="txInterceptor"class="...transaction.interceptor.TransactionInterceptor"autowire="byType">

</bean>

<bean id="txAttributeSource"class="...transaction.interceptor.AttributesTransactionAttributeSource"autowire="constructor">

</bean>

<bean id="attributes"class="...metadata.commons.CommonsAttributes">

</bean>

PlatformTransactionManager

Page 114: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

&8;&8;&8;&8;

Page 115: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

&8;

*&8;

– &!.� �#8��������;�����

* �����#�������������

– +:&– �����– =�!�����

Page 116: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

=�!������ ��2��������

*:�2�����:����� � �����=�!�������������� ���4

<bean id="dataSource" ...> ... </bean>

<bean id="sessionFactory" class="...LocalSessionFactoryBean"><property name="mappingResources">

<list><value>employee.hbm.xml</value>

</list></property><property name="hibernateProperties">

<props><prop key="hibernate.dialect">....DB2Dialect</prop>

</props></property><property name="dataSource">

<ref bean="dataSource"/></property>

</bean>

Page 117: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

=�!�����������

*,�����=�!�����������

*@���U������

SessionFactory sessionFactory =(SessionFactory) ctx.getBean("sessionFactory");

HibernateTemplate hibernate =new HibernateTemplate(sessionFactory);

Employee e = (Employee) hibernate.load(Employee.class, "000330");e.setFirstName("BOB");hibernate.update(e);

Page 118: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

=�!�����������

*S�������������� ��'����� ��������

List employees = hibernate.find("from app.Employee");

List list = hibernate.find("from app.Employee e where e.lastName=?","LEE",Hibernate.STRING);

List list = hibernate.find("from app.Employee e where e.lastName=? and e.firstName=?",new String[] { "BOB", "LEE" },new Type[] {Hibernate.STRING , Hibernate.STRING });

Page 119: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

=�!�����������

*S�������������� ��! �������

List list = (List) hibernate.execute(new HibernateCallback() {public Object doInHibernate(Session session)

throws HibernateException {

List result = session.find("from app.Employee");// do some further stuff with the result list

return result;}

});

Page 120: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�/ ��������������

*���������=�!�������/ �����������:�- ����/ ������������ �4

*C�����������������4��������+:�,

Page 121: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�+��+��+��+�

Page 122: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�����������+��

* ����������������������� ����������� ��!���������������2��+�������4� ���T�����'��$$$

* ����������������������� ����������������+��

Page 123: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�+�� ���

*(�����+������������������'�

– -����'� �@� ���A����� ����2�+E:���������� ����/����+������������

– -���������:������A8��� ��� ������������������������������

*(���������������������������������� ����4

Page 124: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

- ��������@� ���@��

*��������@� ��������������������������������@� ��������������������������������@� ��������������������������������@� ������������������������

*5��� ����������!���������������5��� ����������!���������������5��� ����������!���������������5��� ����������!����������������������� ����������� ������ ����������� ����������� ������ ����������� ����������� ������ ����������� ����������� ������ ���– 0���� ���������������!�������������2 ���������+����� �2� ������2 �1

<bean id="myComponent"class="...ejb.access.LocalStatelessSessionProxyFactoryBean"><property name="jndiName">

<value>myComponent</value></property><property name="businessInterface">

<value>com.mycom.MyComponent</value></property>

</bean>Creates a proxy (the business delegate) that uses a service locator to access the EJB

Creates a proxy (the business delegate) that uses a service locator to access the EJB

Page 125: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

- ��������8������@��

*��������8�����������������������������������8�����������������������������������8�����������������������������������8���������������������������

<bean id="myComponent"class="...SimpleRemoteStatelessSessionProxyFactoryBean"><property name="jndiEnvironment">

<ref bean="myEnvironment"/></property><property name="jndiName">

<value>myComponent</value></property><property name="businessInterface">

<value>com.mycom.MyComponent</value></property>

</bean>

Page 126: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�+�������������

*-!��� ��������������

– @��������� ���4A�+����'�������'��!���ejb/BeanFactoryPath ��� �2���������� ������������ ������ �2���B;@�!���2 ���4���2�������

– �$�$�? �?4 �?4� ���?4!���$/�

A:�2����!���2 ���4����B�-���� ����,����/�

*-���� ����������������������+�����4����2 ��

– ������������� ���2���������!����������� ���4

Page 127: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�������������@��

* ���������������������

*�/�����-!��� �������������������

– �'��������������� ����/�– ���4��������������2��.!8��'�01– �.!,����01������– ��������/ �����������.!- ��'��01�����.!3���'��01

* ��! ���������������������.!,����01

Page 128: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�������������@��

*�/���class MySlsb extends AbstractStatelessSessionBean {

protected void onEjbCreate() throws CreateException {...

}

public void businessMethod() {BeanFactory bf = getBeanFactory();MyBusinessBean mbb = bf.getBean("myBusinessBean");...

}}

Page 129: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

����������������

* ����2���������������

*�/�����-!��� �����2������������

– �'��������������� ����/�– ���4��������������2��.!8��'�01– �.!,����01������

* ��! �������������������.!,����01���.!- ��'��01�����.!3���'��01

Page 130: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

����������������

*�/���class MySfsb extends AbstractStatefulSessionBean {

public void ejbCreate() throws CreateException {loadBeanFactory();...

}public void ejbActivate() {

...}public void ejbPassivate() {

...}

public void businessMethod() {BeanFactory bf = getBeanFactory();MyBusinessBean mbb = bf.getBean("myBusinessBean");...

}}

Page 131: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

������������;:�

*�/���class MyMdb extends AbstractJmsMessageDrivenBean {

protected void onEjbCreate() throws CreateException {...

}

public void onMessage(Message message) {BeanFactory bf = getBeanFactory();MyBusinessBean mbb = bf.getBean("myBusinessBean");...

}}

Page 132: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

(�!(�!(�!(�!

Page 133: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

(�!-���� ����,����/�

*-���� ����� ����/���� ��������������2���

– ������������ ����/���������� ����– :�2���M�?(��#�E�?���� ����,����/�$/�

*,����/�����������!4

– ,����/�@����@��������0���'�����$�1– ,����/�@�������'����0���'�����$71

*,��!�������������4���!�2������

– C��������������4������!��4

Page 134: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

<listener><listener-class>...web.context.ContextLoaderListener</listener-class>

</listener>

(�!-���� ����,����/�

*�/���

– ��!$/�

– �����������'���WebApplicationContextUtils.getWebApplicationContext(ServletContext);

Load root application context from/WEB-INF/applicationContext.xml

Load root application context from/WEB-INF/applicationContext.xml

Page 135: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

(�!�;D,(�!�;D,(�!�;D,(�!�;D,

Page 136: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

�����

*�����

*8�2���� �

– :�'������������������������;D,����� ���������#!4#����

A����M??���$������2������$���?�� �?;D,#����#!4#����?������#;D,#����#!4#����$���

Page 137: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

8������8������8������8������

Page 138: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

8�����/������

*-�4�!���������� ����/�� ��!���/������

*-�8�����/��������/�������!���������������'� �

* �����#�����������2��

– 8;�– +-B#83,– �����– =�����

Page 139: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

8����'� ��/������

*�������'� �����!���/������

* �������'� ���/������

class MyServiceImpl implements MyService {...

}

<bean id="myService" class="app.MyServiceImpl"/>

<bean id="myService-rmi"class="...remoting.rmi.RmiServiceExporter"><property name="service"><ref local="myService"/></property><property name="serviceInterface">

<value>app.MyService</value></property><property name="serviceName">

<value>myService</value></property>

</bean>

Page 140: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

;���$$$;���$$$;���$$$;���$$$

Page 141: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

8����

* �������6$6

– +;���������

– +;B��������

– �� ����'�������#!����'������

– -&3������ ����/�����������������+�8#6PQ����'����

* �������6$�

– &FE@��������

– +,-��������

– ���� ���8;���������

* �������6$7)

– +��

– 3�������

Page 142: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

8���������.� ��

*8� ��,������3��2���0���!�/1

– �������8,3

*D��������0���!�/1

– ,����#'������– -����!����!���

* �� ����4

– - ������ ����4��4����2���������A����M?? ����� ����4$���� �2����$���?

Page 143: Introduction to Spring Framework (Presentation - 143 Slides)

������������� ������������������

8�2���� ��

* �������������!�����– ����M??���$������2������$���?

* -�������!�����0+'�� ������� ��B��2�� ��������������������� $1– ����M??����4 ����$ �$��?������?'��#�������$���

* ;������������ ��'���0������2������#����1– ����M??����$���$���?���$ ��$.'$������2������$����

* �/�����&��#��#&���+����:�'�����������������+��0���!����!������1– �4�8���+��������+<�����=>����