The examples in this article describe the basic configuration and control inversion of spring. Share it with everyone for your reference. The details are as follows:
Here we use maven to build the java project, the same applies to those that do not use maven.
1. Create a maven project. The name of the project I created is springdemo.
2. Add dependent packages. I added the packages through maven. The relevant maven configuration is as follows:
Copy the code 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 entity, dao, business and other package structures.
4. Write simple code
user, empty code, as follows:
Copy the code as follows: package com.chzhao.model;
public class User {
}
interfaceIUserDao
Copy the code as follows: package com.chzhao.dao;
import com.chzhao.model.User;
public interface IUserDao {
public User findUserById();
}
Interface implementation
Copy the code as follows: package com.chzhao.dao;
import com.chzhao.model.User;
public class UserDaoImpl implements IUserDao {
public User findUserById() {
System.out.println("dao");
return null;
}
}
The code to call the copied code is as follows: 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 copy code is as follows: 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 copy code is as follows: <?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"/>
<bean name="userManager">
<property name="dao" >
<ref bean="UserDaoImpl"/>
</property>
</bean>
</beans>
The above is the normal way.
Here's how to annotate.
First, the configuration file needs to be changed. Copy the code as follows: <?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 />
<bean id="UserDaoImpl"/>
<bean name="userManager"> </bean>
</beans>
The interface implementation copy code is as follows: 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;
}
}
The code to call the copied code is as follows: 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();
}
}
Both methods achieve the same thing.
I hope this article will be helpful to everyone’s Java programming.