Section 65: Spring basics of Java back-end learning

Section 65: Spring basics of Java back-end learning

Spring basics for learning Java back-end

If you want to learn spring, then what is the framework, springwhat is it to learn? springIn iocand bean, well aop, IOC, Bean, AOP, (configuration, annotations, api) - springFramework.

Various learning knowledge points:

spring expression language
spring integration
spring web flow
spring security
spring data
spring batch
 

springwebsite: http://spring.io/

http://spring.io/projects/spring-framework

springIt is an open source framework that was created to solve the complexity problem of enterprise application development.Now the development is not limited to enterprise applications.

springIt is a lightweight inversion of control (IoC) and aspect-oriented (AOP) container framework.

A famous saying: springBrings the javaeespring of complex development.

jdbc orm
oxm jms
transactions
websocket servlet
web portlet
aop aspects instrumentation messaging
beans core context spel
 

springmvc+spring+hibernate/ibatis->Enterprise Application

What is a framework and why use a framework:

What is a framework:

A framework is a set of rules and norms formulated by others.Everyone works under this norm or rule. It can be said that others have built the building and let us live.

The software framework is a semi-finished product, with specific processing procedures and control logic, mature software that can be continuously upgraded and improved.

The framework is highly reused, development efficiency and quality are improved, easy to use, and problems can be solved quickly.

spring ioccontainer

The interface is an intermediary for communication, and it is abstract.The javainterface in the middle is to declare which methods are exposed to the outside world.

Interface-oriented programming is used to hide specific implementation components.

Case:

// 
public interface DemoInterface{
 String hello(String text);
// hello , , `String` .
}
// 
public class OneInterface implements DemoInterface{
 @Override
 public String hello(String text){
  return " : " + text;
 }
}
// 
public class Main{
 public static void main(String[] args){
  DemoInterface demo = new OneInterface();
  System.out.println(demo.hello("dashucoding");
 }
}
 

What is IOC, IOCis to control the reverse, what inversion of control, control of the transfer, the application does not depend responsible for creating and maintaining object, but by the external container to create and maintain.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="oneInterface" class="com.ioc.interfaces.OneInterfaceImpl"></bean>
</beans>
 

spring.xml

test:

import org.junit.Test;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestOneInterface extends UnitTestBase {
 public TestOneInterface(){
  super("spring.xml");
 }
 @Test
 public void testHello(){
  OneInterface oneInterface = super.getBean("oneInterface");
  System.out.println(oneInterface.hello("dashucoding"));
 }
}
 

unit test

Download a package and junit-*.jarimport it into the project, and then create a UnitTestBaseclass to springload and destroy the configuration file. All unit tests are inherited UnitTestBase, and then getBeanget the desired object through its method. The subclass should be annotated @RunWith(BlockJUnit4ClassRunner.class), and the unit Annotate the test method @Test.

 public ClassPathXmlApplicationContext context;
 public String springXmlpath;
 public UnitTestBase(){}
 public UnitTestBase(String springXmlpath){
  this.springXmlpath = springXmlpath;
 }
@Before
public void before(){
 if(StringUtils.isEmpty(springXmlpath)){
  springXmlpath = "classpath*:spring-*.xml";
 }
 try{
  context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
  context.start();
 }catch(BeansException e){
  e.printStackTrace();
 }
 }
 @After
 public void after(){
  context.destroy();
 }
 @SuppressWarnings("unchecked")
 protected <T extends Object> T getBean(String beanId){
  return (T)context.getBean(beanId);
 }
 protected <T extends Object> T getBean(Class<T> clazz){
  return context.getBean(clazz);
 }
}
 

beancontainer:

org.springframework.beansAnd org.springframework.context BeanFactoryprovide the configuration structure and basic functions, load and initialize Bean, and ApplicationContextsave the Beanobject.

// 
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:/appcontext.xml");
//Classpath
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
//Web 
<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
 <servlet-name>context</servlet-name>
 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>
 

springInjection: When starting springloading bean, the behavior of assigning values to variables is completed. Injection method: setting injection and construction injection.

// 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="iService" class="com.service.iServiceImpl">
  <property name="iDAO" ref="DAO"/>
 </bean>
 <bean id="DAO" class="com.iDAOImpl"></bean>
</beans>
 
// 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="iService" class="com.service.iServiceImpl">
  <constructor-arg name="DAO" ref="DAO"/>
  <property name="injectionDAO" ref="injectionDAO"></property>
 </bean>
 <bean id="DAO" class="com.iDAOImpl"></bean>
</beans>
 

springinjection:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="injectionService" class="com.injection.service.InjectionServiceImpl"></bean>
 <bean id="injectionDAO" class="com.ijection.dao.InjectionDAOImpl"></bean>
</beans>
 
// - 
public interface InjectionService {
 public void save(String arg);
}
 
// - 
public class InjectionServiceImpl implements InjecionService {
 private InjectionDAO injectionDAO;
 public InjectionServiceImpl(InjectionDAO injectionDAO) {
  this.injectionDAO = injectionDAO;
 }
 public void setInjectionDAO(InjectiionDAO injectionDAO) {
  this.injectionDAO = injectionDAO;
 }
 public void save(String arg) {
  System.out.println(" " + arg);
  arg = arg + ":" + this.hashCode();
  injectionDAO.save(arg);
 }
}
 
// - - DAO
public interface InjectionDAO { 
// 
 public void save(String arg);
}
 
// 
public class InjectionDAOImpl implements InjectionDAO {
// 
 public void save(String arg) {
  System.out.println(" " + arg);
 }
}
 
// 
import org.junit.Test;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase {
 public TestInjection(){
  super("classpath:spring-injection.xml");
 }
 @Test
 public void  testSetter(){
  InjectionService service = super.getBean("injectionService");
  service.save(" ");
 }
 @Test 
 public void testCons() {
  InjectionService service = super.getBean("injectionService");
  service.save(" ");
 }
}
 

beanConfiguration:

id:id ioc ,bean 
class: 
scope: 
constructor  arguments: 
properties: 
autowiring mode: 
lazy-initialization mode: 
Initialization/destruction method: 
 

Scope

singleton: 
prototype: 
request: http 
session: 
 

spring beanConfiguration Awareinterface: The interface ending in springis provided in Awarethe interface, springwhich provides convenience for the expansion.

beanAutomatic assemblyautowiring

no 
byname 
byType bean , bean, , byType , , 
Constructor byType , , bean 
 

spring beanConfiguredresource

resources:
urlresource url 
classpathresource 
filesystemresource 
servletcontextresource servletcontext 
inputstreamresource 
bytearrayresource 
 
public interface ResourceLoader{
 Resource getResource(String location);
}
 

ResourceLoader

classpath: Loaded from the classpath;
file: Loaded as a URL, from the filesystem;
http: Loaded as a URL;
 

Case:

public class MResource implements ApplicationContextAware{
  private ApplicationContext applicationContext;
 @Override
 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
 this.applicationContext = applicationContext;
 }
 public void resource(){
  Resource resource = applicationContext.getResource("classpath:config.txt");
  System.out.println(resource.getFilename());
 }
}
 
// 
import com.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestResource extends UnitTestBase {
 public TestResource() {
  super("classpath:spring-resource.xml");
 }
 @Test
 public void testResource() {
  MResource resource = super.getBean("mResource");
  try{
   resource.resource();
  }catch(IOException e){
   e.printStackTrace();
  }
 }
}
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="moocResource" class="com.resource.MResource"></bean>
</beans>
 

beanDefinition and learning:

<context:annotation-config/>
@Component,@Repository,@Service,@Controller
@Required,@Autowired,@Qualifier,@Resource
 
@Configuration,@Bean,@Import,@DependsOn
@Component,@Repository,@Service,@Controller
 
  1. @RepositoryUsed to annotate the DAOclass as a persistence layer
  2. @ServiceUsed to annotate the Serviceclass as the service layer
  3. @ControllerUsed Controlleras a control layer

Meta-annotations Meta-annotationsare springprovided annotations that can be used as byte codes called metadata annotations, processing value(), meta-annotations can have other attributes.

springCan be automatically detected and registeredbean

@Service
public class SimpleMovieLister {
 private MovieFinder movieFinder;
 @Autowired
 public SimpleMovieLister(MovieFinder movieFinder){
  this.movieFinder = movieFinder;
 }
}
 
@Repository
public class JpaMovieFinder implements MovieFinder {

}
 

Automatic detection and Beanregistration of classes

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 <context:component-scan base-package="org.example"/>
</beans>
 

beanConditions for the class to be automatically discovered and registered :

 @Component,@Repository,@Service,@Controller @Component 
 

@RequiredFor beanattribute settermethod @Autowiredannotations

private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
 this.movieFinder = movieFinder;
}
 
@Autowired
private MovieCatalog movieCatalog;
private CustomePreferenceDap customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
 this.customerPreferenceDao = customerPreferenceDao;
}
 

@Autowiredannotation

Using this annotation, if it is not found, it beanwill cause an exception to be thrown, which can be avoided by using the following code.Each class can only have one constructor marked as required=true.

public class SimpleMovieLister {
 private MovieFinder movieFinder;
 @Autowired(required=false)
 public void setMovieFinder(MovieFinder movieFinder){
  this.movieFinder = movieFinder;
 }
}
 

springIt is an open source framework. Spring is an mvc framework developed with j2ee. Spring boot is a rapid development framework that can integrate components. It is very convenient because it uses maven management. As for spring cloud, it is a microservice framework.

springIt is a lightweight Javadevelopment framework created to solve the complexity of enterprise application development. The framework has the advantage of a layered architecture.

springThis kind of framework is simple, testable and loosely coupled.For the springframework, we mainly learn about inversion of control IOCand aspect-oriented AOP.

// 
spring ioc
spring aop
spring orm
spring mvc
spring webservice
spring transactions
spring jms
spring data
spring cache
spring boot
spring security
spring schedule
 

spring iocFor control inversion, control inversion, control inversion,

SpringContainer is a Springcore framework. springThe container realizes the creation and coordination of interdependent objects. As long as the objects are related to the business logic itself, the IOCmost important thing is to complete the creation of objects and the management injection of dependencies. Inversion of control is to create objects that need to be implemented in the code and rely on them. The code is reversed to the container. This requires the creation of a container to let the container know the relationship between the created object and the object. (Tell spring what you are and what you need)

xml,properties 
classpath,filesystem,servletContext .
 

Inversion of control is to hand over the dependency between objects to the container management, which is originally the dependency between objects managed by the application.

spring iocArchitecture

BeanFactory
BeanDefinition
 

spring iocIt is springone of the cores, and it is also spring , spring bean .spring IOC DI .BeanFactory ,ioc . , new object() .ioc :spring .( spring` to complex control object life cycle and inter-object)

All classes are created and destroyed by the springcontrol is no longer its object referenced by the control object life cycle spring. All objects are springcontrolled.

iocThe interface of the container (design and face each link by yourself):

BeanFactoryFactory mode

public interface BeanFactory {
 String FACTORY_BEAN_PREFIX = "&"; 
 Object getBean(String name) throws BeansException;
 Object getBean(String name, Class requiredType) throws BeansException;

 boolean containsBean(String name); 
 boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
 Class getType(String name) throws NoSuchBeanDefinitionException;
 String[] getAliases(String name); 
}
 

BeanFactory3.subclasses: ListableBeanFactory,HierarchicalBeanFactory and AutowireCapableBeanFactoryrealize the class is DefaultListableBeanFactory.

Inversion of control is that all objects are springcontrolled. iocDynamically provide an object with the objects it needs. It is achieved through DIdependency injection. How to achieve dependency injection ID, Javathere is a feature in reflection, which can be used when the program is running Dynamically generate objects and execute methods of objects, and change the properties of objects.

public static void main(String[] args){
 ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
 Animal animal = (Animal)context.getBean("animal");
 animal.say();
}
 
//applicationContext.xml
<bean id="animal" class="com.test.Cat">
 <property name="name" value="dashu"/>
</bean>
 
public class Cat implements Animal {
 private String name;
 public void say(){
  System.out.println("dashu");
 }
 public void setName(String name){
  this.name = name;
 }
}
 
public interface Animal {
 public void say();
}
 
//bean
private String id;
private String type;
private Map<String,Object> properties=new HashMap<String, Object>();
 
<bean id="test" class="Test">
 <property name="testMap">

 </property>
</bean>
 
public static Object newInstance(String className) {
 Class<?> cls = null;
 Object obj = null;
 try {
  cls = Class.forName(className);
  obj = cls.newInstance();
 } catch (ClassNotFoundException e) {
  throw new RuntimeException(e);
 } catch (InstantiationException e) {
  throw new RuntimeException(e);
 } catch (IllegalAccessException e) {
  throw new RuntimeException(e);
 }
 return obj;
}
 

The core is the inversion of control ( IOC) and the aspect-oriented ( AOP), which springis a layeredJavaSE/EE lightweight open source framework.

web:

struts,spring-mvc
 

service:

spring
 

dao:

mybatis,hibernate,jdbcTemplate,springdata
 

springArchitecture

ioc

// 
public interface UserService {
 public void addUser();
}
// 
public class UserServiceImpl implements UserService {
 @Override
 public void addUser(){
  System.out.println("dashucoding");
 }
}
 

Configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.xsd">
 
	<bean id="userServiceId" class="com.dashucoding.UserServiceImpl"></bean>
</beans>
 

test:

@Test
public void demo(){
	String xmlPath = "com/beans.xml";
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
	UserService userService = (UserService) applicationContext.getBean("userServiceId");
	userService.addUser();
}
 

Dependency injection:

class DemoServiceImpl{
 private daDao daDao;
}
 

Create serviceinstance, create daoinstance, daoset toservice .

Interface and implementation class:

public interface BookDao {
	public void addBook();
}
public class BookDaoImpl implements BookDao {
	@Override
	public void addBook() {
		System.out.println("dashucoding");
	}
}
 
public interface BookService {
	public abstract void addBook();
}
public class BookServiceImpl implements BookService {
	private BookDao bookDao;
	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}
	@Override
	public void addBook(){
		this.bookDao.addBook();
	}
}
 

Configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="bookServiceId" class="com.BookServiceImpl">
		<property name="bookDao" ref="bookDaoId"></property>
	</bean>
	
	<bean id="bookDaoId" class="com.BookDaoImpl"></bean>
</beans>
 

test:

@Test
public void demo(){
	String xmlPath = "com/beans.xml";
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
	BookService bookService = (BookService) applicationContext.getBean("bookServiceId");
		
	bookService.addBook();
}
 

IDEBuild the Springproject

File >new >project >Spring
 

spring

//Server.java
public class Server {
 privete String name;
 public void setName(String name){
  this.name = name;
 }
 public void putName(){
  System.out.println(name);
 }
}
 
//Main.java
public class Main{
 public static void main(String[] args){
  ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
  Server hello = (Server)context.getBean("example_one");
  hello.putName();
 }
}
 

spring-config.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="example_one" class="Server">
  <property name="name" value=" "></property>
 </bean>
</beans>
 

Use Mavento declare Springlibrary Mavenis a project management tool that mavenprovides a developer to build a complete life cycle framework. MavenThe installation and configuration, the need is JDK 1.8, Maven, Windows, configuration jdk, JAVA_HOMEvariable added to the windowsenvironment variable. Download Apache Maven, add M2_HOMEand MAVEN_HOMEadd to Environment variable PATH, the value is %M2_HOME%\bin.executemvn version command to display the result.

MavenEnable proxy for access, find the file roadbed, find/conf/settings.xml , fill in the proxy, and ask Ali.

MavenCentral repository address: https://search.maven.org/

//xml
<dependency>
       <groupId>org.jvnet.localizer</groupId>
        <artifactId>localizer</artifactId>
        <version>1.8</version>
</dependency>
 
//pom.xml
<repositories>
	<repository>
	    <id>java.net</id>
	    <url>https://maven.java.net/content/repositories/public/</url>
	</repository>
</repositories>
 

MavenAdd remote warehouse:

//pom.xml
<project ...>
<repositories>
    <repository>
      <id>java.net</id>
      <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
 </repositories>
</project>

<project ...>
    <repositories>
      <repository>
	<id>JBoss repository</id>
	<url>http://repository.jboss.org/nexus/content/groups/public/</url>
      </repository>
    </repositories>
</project>
 

MavenDependency mechanism, use to Mavencreate Javaproject.

<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.11</version>
	<scope>test</scope>
</dependency>
 

MavenBale:

<project ...>
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.dashucoding</groupId>
	<artifactId>NumberGenerator</artifactId>	
	<packaging>jar</packaging>	
	<version>1.0-SNAPSHOT</version>
 

springframe:

public interface HelloWorld{
 public void sayHello();
}
public class SpringHelloWorld implements HelloWorld {
 public void sayHello(){
  System.out.println("Spring Hello");
 }
}

public class StrutsHelloWorld implements HelloWorld {
 public void sayHello(){
  System.out.println("Struts Hello");
 }
}

public class HelloWorldServie {
 private HelloWorld helloWorld;
 public HelloWorldService(){
  this.helloWorld = new StrutsHelloWorld();
 }
}
 

Inversion of control:

public class HelloWorldService{
 private HelloWorld helloWorld;
 public HelloWorldService(){
 }
 public void setHelloWorld(HelloWorld helloWorld){
  this.helloWorld = helloWorld;
 }
 public HelloWorld getHelloWorld(){
  return this.helloWorld;
 }
}
 

iocThe HelloWorldServiceobject is created .

spring->HelloProgram.java
helloworld->
HelloWorld.java
HelloWorldService.java
impl ->
SpringHelloWorld.java
StrutsHelloWorld.java
resources->beans.xml
// 
 spring:HelloProgram.java
 :
 :
 :beans.xml
 
//HelloWorld.java
public interface HelloWorld {
 public void sayHello();
}
//public class HelloWorldService {
 private HelloWorld helloWorld;
 public HelloWorldService(){
 }
 public void setHelloWorld(HelloWorld helloWorld){
  this.helloWorld = helloWorld;
 }
 public HelloWorld getHelloWorld(){
  return this.helloWorld;
 }
}
 
//SpringHelloWorld.java
public class SpringHelloWorld implements HelloWorld {
  
    @Override
    public void sayHello() {
        System.out.println("Spring Hello!");
    }
}
//StrutsHelloWorld.java
public class StrutsHelloWorld implements HelloWorld {
  
    @Override
    public void sayHello() {
        System.out.println("Struts Hello!");
    }
}
 
//HelloProgram.java
public class HelloProgram {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");
        HelloWorldService service =
             (HelloWorldService) context.getBean("helloWorldService");
        HelloWorld hw= service.getHelloWorld();
        hw.sayHello();
    }
}
 
//beans.xml
<beansxmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
  
    <bean id="springHelloWorld"
        class="com.spring.helloworld.impl.SpringHelloWorld"></bean>
    <bean id="strutsHelloWorld"
        class="com.spring.helloworld.impl.StrutsHelloWorld"></bean>
  
  
    <bean id="helloWorldService"
        class="com.spring.helloworld.HelloWorldService">
        <property name="helloWorld" ref="springHelloWorld"/>
    </bean>
  
</beans>
 
<propertyname="helloWorld"ref="strutsHelloWorld"/>
 

iocCreate an beansimplementation class springHelloWorld, create a helloWorldServiceclass, and beans.xmlimplement parameter import:

//helloWorldService
//springHelloWorld
//Hello Program.java
ApplicationContext context = new ClassPathXmlApplicationContxt("beans.xml");
HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");
HelloWorld hw = service.getHelloWorld();
hw.sayHello();

//HelloWorldService
public class HelloWorldService {
 private HelloWorld helloWorld;
 public HelloWorldService(){
 }
 public void setHelloWorld(HelloWorld helloWorld){
  this.helloWorld = helloWorld;
 }
 public HelloWorld = getHelloWorld() {
  return this.helloWorld;
 }
}
 
//beans.xml
<bean id=" " class=" "/>
<bean id="helloWorldService"
 class="">
 <property name="helloWorld" ref="springHelloWorld"/>
</bean>
 

springLibrary address: http://maven.springframework.org/release/org/springframework/spring/

hello-world:

public class HelloWorld {
	private String name;
	public void setName(String name) {
		this.name = name;
	}
	public void printHello() {
		System.out.println("Spring" + name);
	}
}
 
//xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="helloBean" class="">
		<property name="name" value="dashu"/>
	</bean>

</beans>
 
// 
public class App {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml"); 
                HelloWorld obj = (HelloWorld) context.getBean("helloBean");
		obj.printHello();
	}
}
 

Da Shu Xiaosheng: For the rest of your life, you and me, we are family alone! You and me, we are family! Handsome guy born in the 90s, good development habits; independent thinking ability; active and good at communication . com/u/c785ece60...

Concluding remarks

  • Below I will continue to explain in depth other knowledge, if you are interested, you can continue to pay attention
  • Go for a little gift or like it