spring4 learning (1) basic configuration

Record the basic usage of Spring4.
Environment: Myeclipse10.6+jdk7
Library: spring4.1.6

1. Create a project
Configure Maven-related properties in Myeclipse, and create a Maven-based java project testSpring.

2. Add dependency library
Add spring dependency in pom.xml.
<!-- spring -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.1.6.RELEASE</version>
</dependency>


3. The basic calling method of defining the interface
spring is to load the configuration in the startup program and create the ApplicationContext, obtain the Service Bean and Dao Bean through the ApplicationContext and inject them, and then call the business method in the Bean.
The directory structure is as follows:
src
--main
----java
------com.sunbin.test.testSpring
--------main
---------- Test running class
--------service
----------service interface class
----------impl
------------ service implementation class
--------dao
----------dao interface class
----------impl
------------dao implementation class
----resources
------spring configuration file

Define the service interface TestService.
package com.sunbin.test.testSpring.service;

public interface TestService {
	public String test(String string);
}

Define the dao interface TestDao.
package com.sunbin.test.testSpring.dao;

public interface TestDao {
	public String test(String string);

}


4. Implement the interface
Simple implementation of the service interface.
package com.sunbin.test.testSpring.service.impl;

import com.sunbin.test.testSpring.dao.TestDao;
import com.sunbin.test.testSpring.service.TestService;


public class TestServiceImpl implements TestService {

	private TestDao testDao;
	
	public String test(String string) {
		// TODO Auto-generated method stub
		return "testServiceImpl.test:"+testDao.test(string);
	}

	public TestDao getTestDao() {
		return testDao;
	}

	public void setTestDao (TestDao testDao) {
		this.testDao = testDao;
	}

}

Implement the dao interface.
package com.sunbin.test.testSpring.dao.impl;

import com.sunbin.test.testSpring.dao.TestDao;


public class TestDaoImpl implements TestDao {

	@Override
	public String test(String string) {
		// TODO Auto-generated method stub
		return "testDaoImpl.test:"+string;
	}

}


5. Spring configuration
Create a configuration file root-context.xml under src/main/resources with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
	default-autowire="byName" default-lazy-init="true">
    <import resource="services.xml"/>
    <import resource="daos.xml"/>
</beans>

Load other configuration files in the configuration.
The service layer Bean is defined in services.xml:
<?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"
	default-autowire="byName" default-lazy-init="true">

    <!-- services -->
    <bean id="testService" class="com.sunbin.test.testSpring.service.impl.TestServiceImpl">
    </bean>
</beans>

The dao layer bean is defined in dao.xml:
<?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"
	default-autowire="byName" default-lazy-init="true">

    <!-- daos -->
    <bean id="testDao" class="com.sunbin.test.testSpring.dao.impl.TestDaoImpl">
    </bean>
</beans>

By setting default-autowire="byName", spring will automatically find the bean named testDao (defined in dao.xml), and inject the testService bean through the setter method of the TestServiceImpl class.

6. Test
Create a test class Test.
package com.sunbin.test.testSpring.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sunbin.test.testSpring.service.TestService;


public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "root-context.xml" });
		TestService testService  =(TestService) applicationContext.getBean("testService");
		System.out.println(testService.test("helloWorld"));
	}

}

The test class loads the spring configuration, creates the ApplicationContext, and obtains the Service Bean (Dao Bean is injected).
The results are as follows:
  • testServiceImpl.test:testDaoImpl.test:helloWorld

It indicates that Test, Service, and Dao layers are created, injected, and invoked successfully.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711055&siteId=291194637