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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值