当前spring3.2版本 2种方式,在eclipse中直接运行main都没问题 方式1: 方式2: 使用注解式,在Service中使用@Component Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [a.Serv] is defined: expected single bean but found 0: at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:271) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101) at a.Main.main(Main.java:32) ... 5 more |
|
26分 |
把你的代码和xml都贴出来。
|
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Main { private static ApplicationContext context = null; static final String[] path={"classpath:applicationContext.xml"}; static final String[] classpath={"applicationContext.xml"}; private static void init(){ try { context = new FileSystemXmlApplicationContext(path); } catch (Exception e) { try { context = new FileSystemXmlApplicationContext(classpath); } catch (Exception e2) { try { context = new ClassPathXmlApplicationContext(classpath); } catch (Exception e3) { e3.printStackTrace(); } } } } public static void main(String[] args) { init(); System.out.println(context==null); ((Serv)context.getBean(Serv.class)).t(); } } import org.springframework.stereotype.Component; @Component public class Serv { public void t(){ System.out.println("sssssss"); } } application.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd "> <context:annotation-config /> <aop:aspectj-autoproxy/> <context:component-scan base-package="a" > </context:component-scan> <!-- <bean id="serv" class="a.Serv"></bean> --> </beans> |
|
我试过了。没有问题啊。可以运行
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd "> <context:annotation-config /> <aop:aspectj-autoproxy/> <context:component-scan base-package="com.wanmei.spring.test" > </context:component-scan> <!-- <bean id="serv" class="a.Serv"></bean> --> </beans> package com.wanmei.spring.test; import org.springframework.stereotype.Component; @Component public class Serv { public void t(){ System.out.println("sssssss"); } } package com.wanmei.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Bootstrap { /** * @param args */ public static void main(String[] args) { String configLocation = "E:/workspace_java/test/src/com/wanmei/spring/test/applicationContext.xml"; ApplicationContext context = new FileSystemXmlApplicationContext(configLocation); context.getBean(Serv.class).t(); } } |
|
我是用eclipse导出的,export->runnable->package required
你是这种么 |
|
嗯。我照着你说的样子做了测试。确实,存在你说的问题。具体问题我还没有找到。 |