Get the App
SLTechnology News&Howtos  ›  Development  › 

How does SpringBean get Spring Container Operation

Shulou Source: shulou.com Published: 2022-06-03 09:40:48 11月15日 Update

Editor to share with you SpringBean how to obtain Spring container operation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

The details are as follows:

One configuration

Message

2. Resource files

Hello=\ u6b22\ u8fce\ u4f60\ uff0c {0} now=\ u73b0\ u5728\ u65f6\ u95f4\ u662f\ uff1a {0}

Three Bean

Package org.crazyit.app.service;import org.springframework.context.*;import org.springframework.beans.BeansException;import java.util.Locale;public class Person implements ApplicationContextAware {/ / saves the BeanFactory container as a member variable to save the private ApplicationContext ctx / * the Spring container detects all Bean in the container. If a Bean is found to implement the ApplicationContextAware interface, the Spring container will automatically call the method after the Bean is created. When the method is called, the container itself will be passed to the method as a parameter. * / public void setApplicationContext (ApplicationContext ctx) throws BeansException {this.ctx = ctx;} public void sayHi (String name) {System.out.println (ctx.getMessage ("hello", new String [] {name}, Locale.getDefault (Locale.Category.FORMAT);}

Four test classes

Package lee;import java.io.*;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class SpringTest {public static void main (String [] args) throws Exception {ApplicationContext ctx = new ClassPathXmlApplicationContext ("beans.xml"); Person p = ctx.getBean ("person", Person.class); p.sayHi ("Sun WuKong");}}

The above is all the contents of the article "how to get Spring Container Operations by SpringBean". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Containers methods articles content not much parameters variables most members interfaces documents more knowledge industry resources information information channels channels Sun WuKong reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Linux MariaDB Redmi Huawei