Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does SpringBean get Spring Container Operation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report