Spring quick start example tutorial

  • 2020-04-01 03:38:26
  • OfStack

This article illustrates the basic configuration and inversion of control of spring. Share with you for your reference. The details are as follows:

Here we use maven to build Java projects, and the same applies without maven.

1. Create maven project, the project name I created is springdemo.

2. Add dependency package. I added the package through maven, and the configuration of maven is as follows:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.0.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.0.1.RELEASE</version>
</dependency>

Of course, you can also add jar files directly.

3. Create a simple package structure

I created package structures like entity, dao, business, etc.

Write simple code

User, empty code, as follows:

package com.chzhao.model;
public class User {
}

Interface IUserDao
package com.chzhao.dao;
import com.chzhao.model.User;
public interface IUserDao {
     public User findUserById();
}

Interface implementation

package com.chzhao.dao;
import com.chzhao.model.User;
public class UserDaoImpl implements IUserDao {
    public User findUserById() {
        System.out.println("dao");
        return null;
    }
}

call

package com.chzhao.springdemo;
import com.chzhao.dao.IUserDao;
import com.chzhao.model.User;
public class UserManager {
    public IUserDao getDao() {
        return dao;
    }
    public void setDao(IUserDao dao) {
        this.dao = dao;
    }
    private IUserDao dao; 
    public User findUser() { 
        return dao.findUserById(); 
    } 
}

The main initialization

package com.chzhao.springdemo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App {
    public static void main(String[] args) {         ApplicationContext act = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        UserManager um = (UserManager) act.getBean("userManager");
        um.findUser();
    }
}

The corresponding 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="UserDaoImpl"  class="com.chzhao.dao.UserDaoImpl"/> 
    <bean  name="userManager" class="com.chzhao.springdemo.UserManager"> 
        <property name="dao" > 
            <ref bean="UserDaoImpl"/> 
        </property> 
    </bean> 
</beans>

That's the normal way.

Here's how.

First, the configuration file needs to be changed, as shown below

<?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 class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 
    <bean id="UserDaoImpl"  class="com.chzhao.dao.UserDaoImpl"/> 
    <bean  name="userManager" class="com.chzhao.springdemo.UserManager"> </bean> 
</beans>

Interface implementation

package com.chzhao.dao;
import org.springframework.stereotype.Repository;
import com.chzhao.model.User;
@Repository
public class UserDaoImpl implements IUserDao {
    public User findUserById() {
        System.out.println("dao");
        return null;
    }
}

call

package com.chzhao.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import com.chzhao.dao.IUserDao;
import com.chzhao.model.User;
public class UserManager {     @Autowired
    private IUserDao dao; 
    public User findUser() { 
        return dao.findUserById(); 
    } 
}

The two methods implement the same thing.

I hope this article has been helpful to your Java programming.


Related articles: