I am making a Spring Bean for practice. It is very simple bean setting yet keep showing me an error,
NoSuchBeanDefinitionException: No bean named 'bye2' available
Here is my root-context.xml
file
<bean id="bye2" class="com.jun.test.Bye2">
<property name="one" value="one" />
<property name="two" value="two" />
</bean>
And this is Bean Class,
public class Bye2 {
private String one;
private String two;
public Bye2() {
}
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
And this is the main method that calls the bean.
public class ByeMain {
public static void main(String[] args) {
String Configloc = "classpath:root-context.xml";
ApplicationContext ctx = new AnnotationConfigApplicationContext(Configloc);
Bye2 bye2 = ctx.getBean("bye2", Bye2.class);
bye2.setOne("one");
bye2.setTwo("Two");
System.out.println(bye2.getOne());
System.out.println(bye2.getTwo());
}
What am I doing wrong here?
The usage of AnnotationConfigApplicationContext
is incorrect for your context. You should be using ClassPathXmlApplicationContext
. Check the doc of AnnotationConfigApplicationContext
here:
So please try the below :
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(Configloc);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.