In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use Spring family buckets". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Create a PersonDao interface
Create a package named com.mengma.ioc under the src directory of the project, download: play with the Spring family bucket, then create an interface named PersonDao in the package, and add an add () method to the interface
Package com.mengma.ioc
Public interface PersonDao {
Public void add ()
}
3. Create an interface implementation class PersonDaoImpl
Create the PersonDao implementation class PersonDaoImpl under the com.mengma.ioc package, as shown below after editing.
Package com.mengma.ioc
Public class PersonDaoImpl implements PersonDao {
@ Override
Public void add () {
System.out.println ("save () executed...")
}
}
In the above code, the PersonDaoImpl class implements the add () method in the PersonDao interface and executes the output statement when the method is called.
4. Create a Spring profile
Create the core configuration file applicationContext.xml for Spring under the src directory, and edit it as shown below.
In the above code, line 2-5 is the constraint configuration for Spring, and line 7 represents the creation of an instance of bean with id as personDao in the Spring container, where id represents the unique identifier in the file, and the class attribute indicates the actual fully qualified class name (package name + class name) that specifies the Bean to be instantiated.
It is important to note that the name of the configuration file for Spring is customizable and is usually named applicationContext.xml (or bean.xml).
5. Write test classes
Create the test class FirstTest under the com.mengma.ioc package, and add a method named test1 () to the class, as shown below after editing.
Package com.mengma.ioc
Import org.junit.Test
Import org.springframework.context.ApplicationContext
Import org.springframework.context.support.ClassPathXmlApplicationContext
Public class FirstTest {
@ Test
Public void testl () {
/ / define the path to the Spring configuration file
String xmlPath = "applicationContext.xml"
/ / initialize the Spring container and load the configuration file
ApplicationContext applicationContext = new ClassPathXmlApplicationContext (
XmlPath)
/ / obtain the personDao instance through the container
PersonDao personDao = (PersonDao) applicationContext
.getBean ("personDao")
/ / call the add () method of personDao
PersonDao.add ()
}
}
In the above code, you first define the path to the Spring configuration file, then create the Spring container, then get the personDao instance through the Spring container, and finally call the save () method of the instance.
This is the end of "how to use Spring buckets". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.