1. 程式人生 > >javaSpring使用maven初始搭建步驟java專案

javaSpring使用maven初始搭建步驟java專案

1、在maven專案中修改pom.xml引入關於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、加入必須的配置檔案applicationContext.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">

<!--     <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>
通過引入bean的方式,建立和初始化需要的物件

3、載入其他需要的配置檔案和依賴(如log4j.property和hibernate的dependency)
4、寫java程式碼進行測試

譬如以上配置的兩個bean中,分別配置了Userservice的物件us和UserDao的物件ud,在測試類中,只要UserService service = context.getBean("us", UserService.class);即可一獲得已經定義的UserService物件,在service中存在userDao的屬性,通過service物件呼叫getter方法獲得dao物件(因為到時私有的,所以要get特然獲得),即可使用dao類中的方法。

附簡單的測試程式碼

User類

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類

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類

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;
	}	
}

測試類App.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");
		
		//把IOC容器中的us物件取出來
		UserService service = context.getBean("us", UserService.class);
		
		User user = service.getUserDao().getUserById(10);
		
		System.out.println(user.getName());
	}
}
初學者,大家一起交流,不喜勿噴。