javaSpring使用maven初始搭建步骤java项目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34207422/article/details/78742259
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类

扫描二维码关注公众号,回复: 4457273 查看本文章

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());
	}
}
初学者,大家一起交流,不喜勿喷。

猜你喜欢

转载自blog.csdn.net/qq_34207422/article/details/78742259
今日推荐