A bunch of questions on Spring 3 framework

十年热恋 提交于 2019-12-06 08:33:44

问题


Here are the questions resulted from reading the Spring Reference, please help.

(1) Do I ever need manual creation of ApplicationContext? Do I ever need second instance of AplicationContext?

(2) We have the following config instructions:

<context:annotation-config/> 
<context:component-scan base-package=".."/> 
<mvc:annotation-driven/>

Do these instructions duplicate theirselfs? In which cases yes, in which no?

(3) I am a bit stuck with all that ways Spring introduces to convert from string to object: PropertyEditor, Conversions, Formatting.. Here is a simple use case: I have a Spring MVC controller that processes some POST request. That request is a result of filling some form. The form is a web representation of some entity. So, given a the user submits a new Project form. In that form exist a date field and a manager's name field to be selected from the list of existing managers. The entered date should be converted to Date property of Project object, and manager's name - to Manager property, created or located by this name (i.e. I want to inject Manager into his Project). What should I use in this case? Property editors, formatters, something else?

(4) Generally, may I say that all the @interface classes that are found on classpath can be used by Spring as annotations? In other words, how can I know which annotations can be used in my project? All that can be found in my classpath, or I need to register them somehow?

(5) I tried to use spring aop without aspectj.jar: just created an Aspect and addred XML definition for this aspect (without any annotations). As a result it throws "class not found Exception: org/aspectj/weaver/BCException". So looks like I cannot use Spring AOP without aspectJ library?


回答1:


(1) Do I ever need manual creation of ApplicationContext? Do I ever need second instance of AplicationContext?

Spring is typically usd in two environments - in web development and in desktop applications/standalone servers. In the former case the ApplicationContext is created automatically via ContextLoaderListener defined in web.xml or WebContextInitializer in Servlet 3.0 container.

In the latter case (standalone application) you are responsible for creating and destroying the application context.


(2) We have the following config instructions:

<context:component-scan base-package=".."/> provides all the functionality of <context:annotation-config/> plus (surprise!) component scanning. <mvc:annotation-driven/> is completely independent and it recognizes spring-mvc annotations like @Controller.


[...]The entered date should be converted to Date property of Project object[...]

Register custom editor within @Controller:

@Controller
public class FooController {
    @InitBinder
    public void binder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            public void setAsText(String value) {
                try {
                    setValue(new SimpleDateFormat("yy-MM-dd HH:mm:ss").parse(value));
                } catch (ParseException e) {
                    setValue(null);
                }
            }
        });
    }
}

[...]how can I know which annotations can be used in my project?[...]

I found this awesome annotations support sheet some time ago (I am not an author). It will tell you which annotations are enabled when.


All that can be found in my classpath

@Inject is enabled if it is found on the CLASSPATH, other annotations need to be enabled manually, see above.


So looks like I cannot use Spring AOP without aspectJ library?

You can use Spring without CGLIB if you only use interface proxies (i.e. you only apply aspects on classes implementing at least one interface). Otherwise you need CGLIB to dynamically create subclasses.



来源:https://stackoverflow.com/questions/10565481/a-bunch-of-questions-on-spring-3-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!