Spring Dependency Injection Setter injection (via get and set method injection)
Import the necessary jar packages (Spring.jar and Commonslogging.jar)
Build Applicationcontext.xml (Spring Management Bean configuration file) in src directory
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Beans Public "-//speing//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd "><Beans> <BeanID= "User"class= "Com.cc8w.entity.User"> < Propertyname= "Names"> <value>1111</value> </ Property> < Propertyname= "Ages"> <value>30</value> </ Property> </Bean></Beans>
2.Setter Injection
Packagecom.cc8w;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.cc8w.entity.User; Public classTest1 { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext context =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); User User= (user) Context.getbean ("user")); SYSTEM.OUT.PRINTLN (user); }}
Data types for Java
Packagecom.cc8w.entity; Public classUser {PrivateString names; Private intages; PublicString GetNames () {returnnames; } Public voidsetnames (String names) { This. Names =names; } Public intgetages () {returnages; } Public voidSetages (intages) { This. ages =ages; } }
Results:
Spring Dependency Injection setter injection (via get and set method injection)