Expert.One.on.one.J2EE.Development.Without.EJB筆記

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.