论坛首页 Java企业应用论坛

spring3零配置注解实现Bean定义(包括JSR-250、JSR-330)

浏览 71110 次
精华帖 (0) :: 良好帖 (4) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-03-25  
mzba520 写道
楼主,我有一问题请假,我的配置文件加了类似:
<mvc:resources mapping="/images/**" location="/images/"/>
这样的话,我的静态资源可以访问了,但是访问所有controller的时候却报404错误,
没有加的话,访问controller正常。请问是什么原因?


请添加 <mvc:default-servlet-handler/> 

http://www.iteye.com/topic/1120924
0 请登录后投票
   发表时间:2012-03-25  
jinnianshilongnian 写道
mzba520 写道
楼主,我有一问题请假,我的配置文件加了类似:
<mvc:resources mapping="/images/**" location="/images/"/>
这样的话,我的静态资源可以访问了,但是访问所有controller的时候却报404错误,
没有加的话,访问controller正常。请问是什么原因?


请添加 <mvc:default-servlet-handler/> 

http://www.iteye.com/topic/1120924

 

楼主您好,这样还是不行

 

 <mvc:default-servlet-handler/> 
    <mvc:resources mapping="/ckeditor/**" location="/ckeditor/"/>
    <mvc:resources mapping="/lvxing/**" location="/lvxing/"/>
    <mvc:resources mapping="/commons/**" location="/commons/,classpath:/commons/"/>
    <mvc:resources mapping="/gxt/**" location="classpath:/gxt/"/>
    <mvc:resources mapping="/page/**" location="/page/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/upload/**" location="/upload/"/>
    <mvc:resources mapping="/*.jsp" location="/"/>
    <mvc:resources mapping="/*.ico" location="/"/>
    <mvc:resources mapping="/*.txt" location="/"/>
    <mvc:resources mapping="/*.zip" location="/"/>
    <mvc:resources mapping="/*.css" location="/"/>

 

结果是:

 


  • 大小: 26.3 KB
0 请登录后投票
   发表时间:2012-03-26  
mzba520 写道
jinnianshilongnian 写道
mzba520 写道
楼主,我有一问题请假,我的配置文件加了类似:
<mvc:resources mapping="/images/**" location="/images/"/>
这样的话,我的静态资源可以访问了,但是访问所有controller的时候却报404错误,
没有加的话,访问controller正常。请问是什么原因?


请添加 <mvc:default-servlet-handler/> 

http://www.iteye.com/topic/1120924

 

楼主您好,这样还是不行

 

 

 <mvc:default-servlet-handler/> 
    <mvc:resources mapping="/ckeditor/**" location="/ckeditor/"/>
    <mvc:resources mapping="/lvxing/**" location="/lvxing/"/>
    <mvc:resources mapping="/commons/**" location="/commons/,classpath:/commons/"/>
    <mvc:resources mapping="/gxt/**" location="classpath:/gxt/"/>
    <mvc:resources mapping="/page/**" location="/page/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/upload/**" location="/upload/"/>
    <mvc:resources mapping="/*.jsp" location="/"/>
    <mvc:resources mapping="/*.ico" location="/"/>
    <mvc:resources mapping="/*.txt" location="/"/>
    <mvc:resources mapping="/*.zip" location="/"/>
    <mvc:resources mapping="/*.css" location="/"/>

 

结果是:

 


 

能否把你核心配置 打个包 发上来 或发我短消息 具体看下原因。目前来看 可能是您HanlderMapping 配置有问题

0 请登录后投票
   发表时间:2012-05-29  
本来就是要用配置文件的事情,问什么非要去用注解
0 请登录后投票
   发表时间:2012-09-20  
楼主,我现在自己写了个程序,使用spring的注解。但出了问题,能不能帮我看下?
接口

public interface ServiceBean {

public void get();
}

具体类

@Service
public class ServiceBeanImpl implements ServiceBean {

@Override
public void get() {
System.out.println("这是ServiceBean接口的实现类");
}

}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true">

<!--开启注解-->
<context:annotation-config />

<context:component-scan base-package="com.yougou">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
<bean id = "sb" name ="sb" class ="com.yougou.impl.ServiceBeanImpl">
</bean>
-->
</beans>

测试类
@Component
public class Test {

@Autowired(required=true)
private ServiceBean sb;

public void done() {
System.out.println(sb);
}

public Test(){
this.done();
}

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(ctx.getBeanDefinitionNames());
Test test = new Test();
}
}
测试结果是:
[Ljava.lang.String;@b30913
null
明明我已经对sb对象进行了注入,为什么还是null?
0 请登录后投票
   发表时间:2012-09-20  
太阳神喻 写道
本来就是要用配置文件的事情,问什么非要去用注解

因我找这个问题找了很久了。请帮我看看吧,谢谢了
0 请登录后投票
   发表时间:2012-09-20  
song19850712 写道
楼主,我现在自己写了个程序,使用spring的注解。但出了问题,能不能帮我看下?
接口

public interface ServiceBean {

public void get();
}

具体类

@Service
public class ServiceBeanImpl implements ServiceBean {

@Override
public void get() {
System.out.println("这是ServiceBean接口的实现类");
}

}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true">

<!--开启注解-->
<context:annotation-config />

<context:component-scan base-package="com.yougou">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
<bean id = "sb" name ="sb" class ="com.yougou.impl.ServiceBeanImpl">
</bean>
-->
</beans>

测试类
@Component
public class Test {

@Autowired(required=true)
private ServiceBean sb;

public void done() {
System.out.println(sb);
}

public Test(){
this.done();
}

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(ctx.getBeanDefinitionNames());
Test test = new Test();
}
}
测试结果是:
[Ljava.lang.String;@b30913
null
明明我已经对sb对象进行了注入,为什么还是null?



犯晕了吧

Test test = new Test();  怎么能直接new呢 从容器去拿
0 请登录后投票
   发表时间:2012-09-21  
那我现在想通过注入实例化sb对象,应该怎么办?
jinnianshilongnian 写道
song19850712 写道
楼主,我现在自己写了个程序,使用spring的注解。但出了问题,能不能帮我看下?
接口

public interface ServiceBean {

public void get();
}

具体类

@Service
public class ServiceBeanImpl implements ServiceBean {

@Override
public void get() {
System.out.println("这是ServiceBean接口的实现类");
}

}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true">

<!--开启注解-->
<context:annotation-config />

<context:component-scan base-package="com.yougou">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
<bean id = "sb" name ="sb" class ="com.yougou.impl.ServiceBeanImpl">
</bean>
-->
</beans>

测试类
@Component
public class Test {

@Autowired(required=true)
private ServiceBean sb;

public void done() {
System.out.println(sb);
}

public Test(){
this.done();
}

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(ctx.getBeanDefinitionNames());
Test test = new Test();
}
}
测试结果是:
[Ljava.lang.String;@b30913
null
明明我已经对sb对象进行了注入,为什么还是null?



犯晕了吧

Test test = new Test();  怎么能直接new呢 从容器去拿

0 请登录后投票
   发表时间:2012-09-21  
song19850712 写道
那我现在想通过注入实例化sb对象,应该怎么办?
jinnianshilongnian 写道
song19850712 写道
楼主,我现在自己写了个程序,使用spring的注解。但出了问题,能不能帮我看下?
接口

public interface ServiceBean {

public void get();
}

具体类

@Service
public class ServiceBeanImpl implements ServiceBean {

@Override
public void get() {
System.out.println("这是ServiceBean接口的实现类");
}

}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true">

<!--开启注解-->
<context:annotation-config />

<context:component-scan base-package="com.yougou">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
<bean id = "sb" name ="sb" class ="com.yougou.impl.ServiceBeanImpl">
</bean>
-->
</beans>

测试类
@Component
public class Test {

@Autowired(required=true)
private ServiceBean sb;

public void done() {
System.out.println(sb);
}

public Test(){
this.done();
}

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(ctx.getBeanDefinitionNames());
Test test = new Test();
}
}
测试结果是:
[Ljava.lang.String;@b30913
null
明明我已经对sb对象进行了注入,为什么还是null?



犯晕了吧

Test test = new Test();  怎么能直接new呢 从容器去拿



从ctx.getBean(Test.class)
0 请登录后投票
   发表时间:2012-09-21  
谢谢了
jinnianshilongnian 写道
song19850712 写道
那我现在想通过注入实例化sb对象,应该怎么办?
jinnianshilongnian 写道
song19850712 写道
楼主,我现在自己写了个程序,使用spring的注解。但出了问题,能不能帮我看下?
接口

public interface ServiceBean {

public void get();
}

具体类

@Service
public class ServiceBeanImpl implements ServiceBean {

@Override
public void get() {
System.out.println("这是ServiceBean接口的实现类");
}

}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true">

<!--开启注解-->
<context:annotation-config />

<context:component-scan base-package="com.yougou">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
<bean id = "sb" name ="sb" class ="com.yougou.impl.ServiceBeanImpl">
</bean>
-->
</beans>

测试类
@Component
public class Test {

@Autowired(required=true)
private ServiceBean sb;

public void done() {
System.out.println(sb);
}

public Test(){
this.done();
}

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
System.out.println(ctx.getBeanDefinitionNames());
Test test = new Test();
}
}
测试结果是:
[Ljava.lang.String;@b30913
null
明明我已经对sb对象进行了注入,为什么还是null?



犯晕了吧

Test test = new Test();  怎么能直接new呢 从容器去拿



从ctx.getBean(Test.class)

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics