javaSpring uses maven's initial build step java project

Posted by rockintyler on Sun, 05 Jul 2020 16:37:42 +0200

1. Modify in maven projectPom.xmlIntroduce dependencies on spring.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.iflytek</groupId>
  <artifactId>firstSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
   <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.13.RELEASE</version>
        <!--  <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>  -->
    </dependency>
	  <dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring-context</artifactId>
	        <version>4.3.13.RELEASE</version>
	        <scope>runtime</scope>
	   </dependency>
	   
	<dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  </dependencies>
</project>


2. Add required profilesApplicationContext.xmlfile

<?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="..." class="...">
        collaborators and configuration for this bean go here
    </bean>

    <bean id="..." class="...">
        collaborators and configuration for this bean go here
    </bean> -->

    <!-- more bean definitions go here 
    	UserDao ud = new UserDao();
    -->
	<bean id="ud" class="com.iflytek.dao.UserDao"></bean>
	
	<bean id="us" class="com.iflytek.service.UserService">
		<property name="userDao" ref="ud"></property>
	</bean>

</beans>
Create and initialize required objects by introducing bean s

3. Load other required profiles and dependencies (such as log4j.property and hibernate dependency)
4. Write java code for testing

For example, in the two bean s configured above, the object us of Userservice and the object ud of UserDao are configured respectively. In the test class, as long as UserService service =Context.getBean("us"),UserService.class;This means that once you get a defined UserService object, there is a property of userDao in the service, and you call the getter method through the service object to get the dao object (because it is private at that time, you want the get to get it explicitly), you can use the method in the dao class.

Attach simple test code

User class

package com.iflytek.domain;

public class User {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}

}
UserService class

package com.iflytek.service;

import com.iflytek.dao.UserDao;

public class UserService {

	private UserDao userDao1;

	public UserDao getUserDao() {
		return userDao1;
	}

	public void setUserDao(UserDao userDao1) {
		this.userDao1 = userDao1;
	}

}

UserDao class

package com.iflytek.dao;

import com.iflytek.domain.User;

public class UserDao {

	public User getUserById(int i) {
		
		User user = new User();
		user.setId(10);
		user.setName("zhangsan");
		return user;
	}	
}

Test ClassApp.class
package com.iflytek.test;

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

import com.iflytek.domain.User;
import com.iflytek.service.UserService;

public class App {
public static void main(String[] args) {
		
		
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//Remove the us object from the IOC container
		UserService service = context.getBean("us", UserService.class);
		
		User user = service.getUserDao().getUserById(10);
		
		System.out.println(user.getName());
	}
}
Beginner, everyone communicates, don't want to spray.

Topics: Maven Spring Apache log4j