获取org.springframework.beans.factory.NoSuchBeanDefinitionException

时间:2018-01-04 18:55:41

标签: java spring spring-mvc

我是春天的新手并且遇到了一个小问题。

你可以告诉我我在哪里弄错了吗?

代码:

public class GlobalClass 
{
    private  static JdbcTemplate jdbcTemplate;
    private static DataSource dataSource;

    public static JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    @Autowired
    public static void setJdbcTemplate(JdbcTemplate jd) 
    {
        jdbcTemplate = jd;
    }
    public static  DataSource getDataSource() 
    {
        return dataSource;
    }
    @Autowired
    public static void setDataSource(DataSource datasrc) 
    {
        dataSource = datasrc;

    }

    public static void load()
    {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("**bean.xml");
        GlobalClass gs = (GlobalClass) ctx.getBean("Global");

    }
}

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.oracledriver"/>
    <property name="url" value="jdbc:thin//localhost:1521/xe"/>
    <property name="username" value="system"/>
    <property name="password" value="root"/>
</bean>


<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="dataSource"></property>  
</bean> 

<bean id="Global" class="com.example.springtransaction.GlobalClass">  
<property name="jdbcTemplate" ref="jdbcTemplate"></property>  
</bean> 

运行此程序时获取异常没有bean命名。

线程中的异常&#34; main&#34; org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为&#39; Global&#39;可用     在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)     在org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1205)     在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)     在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)     在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1085)     在com.example.springtransaction.GlobalClass.load(GlobalClass.java:40)     在com.example.springtransaction.App.main(App.java:11)​​

0 个答案:

没有答案