其他分享
首页 > 其他分享> > 关于Spring 3框架的一堆问题

关于Spring 3框架的一堆问题

作者:互联网

以下是阅读Spring Reference产生的问题,请帮忙.

(1)我是否需要手动创建ApplicationContext?我是否需要第二个AplicationContext实例?

(2)我们有以下配置说明:

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

这些说明是否与自己复制?在哪些情况下是的,其中没有?

(3)我对Spring引入的从字符串转换为对象的所有方式都有点困惑:PropertyEditor,Conversions,Formatting ..
这是一个简单的用例:
我有一个Spring MVC控制器来处理一些POST请求.该请求是填写某种形式的结果.表单是某个实体的Web表示.
因此,给定用户提交新的项目表单.在该表单中,存在一个日期字段和一个管理者名称字段,可从现有管理者列表中选择.输入的日期应该转换为Project对象的Date属性,以及管理者的名称 – Manager属性,由该名称创建或定位(即我想将Manager注入他的Project).在这种情况下我应该使用什么?属性编辑器,格式化程序,还有别的什么?

(4)通常,我可以说在类路径中找到的所有@interface类都可以被Spring用作注释吗?
换句话说,我怎么知道我的项目中可以使用哪些注释?所有这些都可以在我的类路径中找到,或者我需要以某种方式注册它们?

(5)我试图在没有aspectj.jar的情况下使用spring aop:刚刚为这个方面创建了一个Aspect和addred XML定义(没有任何注释).结果它抛出“class not found Exception:org / aspectj / weaver / BCException”.
所以看起来我不能在没有aspectJ库的情况下使用Spring AOP?

解决方法:

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

Spring通常用于两种环境 – Web开发和桌面应用程序/独立服务器.在前一种情况下,ApplicationContext是通过web.xml中定义的ContextLoaderListener或Servlet 3.0容器中的WebContextInitializer自动创建的.

在后一种情况下(独立应用程序),您负责创建和销毁应用程序上下文.

(2) We have the following config instructions:

< context:component-scan base-package =“..”/>提供< context:annotation-config />的所有功能.加(惊喜!)组件扫描. &LT MVC:注解驱动/&GT是完全独立的,它识别像@Controller这样的注释.

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

在@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?[…]

我前段时间发现了这个awesome annotations support sheet(我不是作者).它将告诉您何时启用了哪些注释.

All that can be found in my classpath

如果在CLASSPATH上找到@Inject,则启用@Inject,需要手动启用其他注释,请参见上文.

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

如果只使用接口代理,则可以在没有CGLIB的情况下使用Spring(即,您只在实现至少一个接口的类上应用方面).否则,您需要CGLIB来动态创建子类.

标签:spring-aop,spring,spring-mvc,spring-annotations,spring-mvc
来源: https://codeday.me/bug/20190729/1573207.html