用Spring框架进行开发的时候,需要怎么做,下面我来模拟一下!

 基本的项目架构如下图所示:

Spring测试

spring.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
		        http://www.springframework.org/schema/context
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <import resource="spring-*.xml"></import>

</beans>

User类

package com.jie.domain;

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

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public User() {
    }
}

 

UserDao接口

package com.jie.dao;

import com.jie.domain.User;

import java.util.List;

public interface UserDao {
    public void hello();
    public void hello(int id);
    public void hello(User user);
    public void hello(List list);

}

UserDaoImpl实现类

package com.jie.dao.impl;

import com.jie.dao.UserDao;
import com.jie.domain.User;

import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public void hello() {

        System.out.println("This is UserDaoImpl.class to say 'Hello World'");
    }

    @Override
    public void hello(int id) {
        System.out.println("This is UserDaoImpl.class to say 'Hello World' and my ID--->"+id);
    }

    @Override
    public void hello(User user) {
        System.out.println("This is UserDaoImpl.class to say 'Hello World'-----user====>"+user.getName());
    }

    @Override
    public void hello(List list) {
        System.out.println("This is UserDaoImpl.class to say 'Hello World'---->"+list.get(0));
    }
}

UserService接口类

package com.jie.service;

import com.jie.domain.User;

import java.util.List;

public interface UserService {
    public void hello();
    public void hello(int id);
    public void hello(User user);
    public void hello(List list);
}

UserServiceImpl实现类

package com.jie.service.impl;

import com.jie.dao.impl.UserDaoImpl;
import com.jie.domain.User;
import com.jie.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDaoImpl userDao;
    private List list;
    @Override
    public void hello() {
        userDao.hello();
    }

    @Override
    public void hello(int id) {
        userDao.hello(id);
    }

    @Override
    public void hello(User user) {
        userDao.hello(user);
    }

    @Override
    public void hello(List list) {
        userDao.hello(this.list);
    }

    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }

    public void setList(List list) {
        this.list = list;
    }

    public List getList() {
        return list;
    }
}

spring的其他配置文件

spring-domain.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
		        http://www.springframework.org/schema/context
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <bean class="com.jie.domain.User" id="user">
        <property name="name" value="xiaojiejie"></property>
    </bean>

</beans>

spring-dao.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
		        http://www.springframework.org/schema/context
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="userDao" class="com.jie.dao.impl.UserDaoImpl"></bean>

</beans>

spring-service.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		        http://www.springframework.org/schema/beans/spring-beans.xsd
		        http://www.springframework.org/schema/context
		        http://www.springframework.org/schema/context/spring-context.xsd
		        http://www.springframework.org/schema/mvc
		        http://www.springframework.org/schema/mvc/spring-mvc.xsd"
><!--default-autowire="byName"-->

<!--
autowire 属性值的自动装配
         默认是no(不装配)
         byName通过name属性名装配
         byType通过类型装配
         default跟随上级的特性
 -->
<bean id="userService" class="com.jie.service.impl.UserServiceImpl" autowire="no">

    <property name="userDao" ref="userDao"></property>

    <property name="list">
        <list>
            <value>剑侠情缘</value>
            <value>葬剑山庄</value>
        </list>
    </property>
</bean>

</beans>

TestUser测试类

package com.jie;

import com.jie.domain.User;
import com.jie.service.UserService;
import com.jie.service.impl.UserServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestUser {

    @Test
    public void test1(){
        BeanFactory factory=new ClassPathXmlApplicationContext("/spring.xml");
        UserServiceImpl userService=(UserServiceImpl) factory.getBean("userService");
        userService.hello();
        userService.hello(22);
        User user=(User)factory.getBean("user");
        userService.hello(user);
        List list  =  userService.getList();
        userService.hello(list);
    }
}

结果:

Spring测试

相关文章:

  • 2022-12-23
  • 2021-12-09
  • 2021-09-04
  • 2021-11-04
  • 2021-05-05
  • 2021-08-07
  • 2021-11-28
  • 2022-03-01
猜你喜欢
  • 2021-12-22
  • 2021-07-22
  • 2021-06-16
  • 2021-10-12
相关资源
相似解决方案