Spring parent-child container and reading properties file

To read the contents of the properties file, you can use @Value

for example

@Value("${url}")
public String url;

The content of the properties file is url=xxxxxx

But it can only be used in the container it is in. For example, the properties file is loaded in the spring container, but if you use this annotation in the Controller of the springmvc container (sub-container), it cannot be obtained.

-------------------------------------------------

About parent-child containers:

The child container can access the resources of the parent container, and the parent container can access the resources of the child container.

What we actually configure in web.xml is the parent-child container org.springframework.web.context.ContextLoaderListener and org.springframework.web.servlet.DispatcherServlet. Because our param-value in DispatcherServlet is spring-servlet.xml, it is useless even if you load controller in applicationContext.xml, it is only useful to load controller in spring-servlet.xml.

-------------------------------------------------

Because of the usage of @Value("${url}"). So there are two ways to use,

First, if you want to use @Value in the subcontainer, you have to import the properties file in spring-servlet.xml.

Second, because the sub-container can access the resources of the parent container, create a class annotated with @Service or @Repository in the sub-container to specifically encapsulate the information in the properties file. Then the subcontainer can get the information in the properties file through the @Autowired object.

The second method is recommended.

 

http://blog.csdn.net/u011302734/article/details/75503467

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326571607&siteId=291194637