Article From:

This is the configuration in webxml

        classpath:application-context.xml </param-value>



How to get bean through controller? If you use ClassPathApplicationContext to get the wrong report, what can I do? Who can help your big brother

Answer 0:

Getting bean in controller can use annotations:
1.Add to your Spring configuration <context:component-scan base-package="" annotation-config="true"/>
2.For example, you need to get a Bean of userService

    protected UserService userService;

3.The premise needs to be added to the annotation class you need@service

public class UserService

You can get it

Answer 1:

Write a springUtils class
Add this bean to the XML of spring
<bean class=””/>

springUtilsInherit BeanFactoryPostProcessor classes
private static ConfigurableListableBeanFactory beanFactory; // SpringApplication context environment
Add this property (getter and setter do not write)
The following method is to get bean
public static <T> T getBean(String name) throws BeansException {

    return (T) beanFactory.getBean(name);

Leave a Reply

Your email address will not be published. Required fields are marked *