CSDN博客

img gaagaa2004

Expert.One.on.one.J2EE.Development.Without.EJB笔记

发表于2005/1/3 20:44:00  1490人阅读

Spring:IOC+AOP

1.如何通过bean factory读取bean(p178)
public class TextStyle {
private String fontName = “default”;
private int fontSize = 9;
private boolean bold = false;
private boolean italic = false;
public void setFontName(String fontName) {
this.fontName = fontName;
}
public String getFontName() {
return fontName;
}
...
}

<beans>
<bean id=”myStyle” class=”style.TextStyle”>
<property name=”fontName”><value>Arial</value></property>
<property name=”fontSize”><value>12</value></property>
</bean>
<bean id=”yourStyle” class=”style.TextStyle”>
<property name=”fontName”><value>Times</value></property>
<property name=”fontSize”><value>10</value></property>
<property name=”bold”><value>true</value></property>
<property name=”italic”><value>true</value></property>
</bean>
</beans>


Resource resource = new ClassPathResource(“styles.xml”); - 1
ListableBeanFactory bf = new XmlBeanFactory(resource);  - 2


TextStyle myStyle = (TextStyle) bf.getBean(“myStyle”);
TextStyle yourStyle = (TextStyle) bf.getBean(“yourStyle”);

1,2 can be replaced to

NO-XML:
ListableBeanFactory bf = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(bf);
Resource resource = new ClassPathResource(“styles.properties”);
reader.loadBeanDefinitions(resource);

XML:
ListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
Resource resource = new ClassPathResource(“styles.xml”);
reader.loadBeanDefinitions(resource);

2.Autowire(p185)
2.1 byType 避免显示的在配置中设置ref,而是根据bean的属性类型自动查找。
<bean id=”myProductManager” class=”ebusiness.DefaultProductManager” autowire=”byType”>
2.2 byName 根据bean的属性名匹配。

3.Dependency checking 类型检查
support:simple(primitives and Strings);objects;all(simple & objects)
e.g
<bean id=”myProductManager” class=”ebusiness.DefaultProductManager”
autowire=”byType” dependency-check=”objects”>

5.Constructor wiring up
IOC 包括Dependency Look up(EJB) + Dependency Injection
Dependency Injection 包括 Setting Injection + Constructor Injection

Spring bean factory can also provide autowiring for constructors,define sequence index.
<beans>
<bean id=”myInventoryManager” class=”ebusiness.DefaultInventoryManager”/>
<bean id=”myProductManager” class=”ebusiness.DefaultProductManager”>
<constructor-arg index=”0”>
<ref bean=”myInventoryManager”/>
</constructor-arg>
<constructor-arg index=”1”>
<value>true</value>
</constructor-arg>
</bean>
</beans>

6.Lifecycle Callbacks(p188)
6.1 Two callback interfaces provided after property setting and on bean factory shutdown.
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}

public interface DisposableBean {
void destroy() throws Exception;
}

6.2 Declarative specification of callback methods,not involve Spring dependencies.
<bean id=”myInventoryManager” class=”ebusiness.DefaultInventoryManager”
init-method=”initialize” destroy-method=”shutdown”/>

7. Complexy property value.(p190)
Bean factory support complexy property value such as List,Map,Array etc.

8.JNDI Resource(p199)
<beans>
<bean id=”myDataSource”
class=”org.springframework.jndi.JndiObjectFactoryBean”>
<property name=”jndiName”>
<value>jdbc/myds</value>
</property>
</bean>
<bean id=”myInventoryManager” class=”ebusiness.DefaultInventoryManager”>
<property name=”dataSource”>
<ref bean=”myDataSource”/>
</property>
</bean>
</beans>

Note that the JNDI name “jdbc/myds” will get resolved to “java:comp/env/jdbc/myds”.

9.ServletContext
Spring WebApplicationContext 
/WEB-INF/applicationContext.xml.

public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
 ServletContext sc = getServletContext();
 WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc);
 MyService myService = (MyService) wac.getBean(“myService”);
 request.setAttribute(“message”, myService.getMessage());
 request.setAttribute(“headline”, myService.getHeadline());
 request.getRequestDispatcher(“/myview.jsp”).forward(request, response);
}
}

10.Struct
Action -- controller
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
Action will automatically receive a ActionForm (if configured accordingly in strutsconfig.xml)

ActionForm -- model
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request):
invoked after population with request parameters but before the invocation of execute.

controller    Action
interceptor —
command/form    ActionForm
validator    ActionForm or declarative rules
validation errors holder  ActionErrors
model     ActionForm, manual request attributes
view reference    ActionForward

11.Spring MVC
controller    Controller, or any handler Object with an adapter
interceptor    HandlerInterceptor, or AOP MethodInterceptor
command/form    Any command or form JavaBean
validator    Validator
validation errors holder  Errors
model     Map of arbitrary model objects in ModelAndView
view reference    View instance or view name in ModelAndView

12.request-driven & event-driven
request-driven:Struct;Spring;WebWork2
event-driven:Tapestry;Java Server Face. (similar to .Net) RAD

0 0

相关博文

我的热门文章

img
取 消
img即使是一小步
也想与你分享
打开
img