JavaEE框架整合开发入门到实战:Spring+SpringMVC+MyBAtis(微课版)——代码练习第三章

发布时间:2022-09-15 03:00

目录结构

JavaEE框架整合开发入门到实战:Spring+SpringMVC+MyBAtis(微课版)——代码练习第三章_第1张图片

package annotation;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AnnotationUser {
	@Value("一棵小白杨")
	private String uname;

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}
	
}

package annotation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import annotation.service.TestService;

@Controller
public class TestController {
  @Autowired
  private TestService testService;
  public void save() {
	  testService.save();
	  System.out.println("testController save");
  }
}

package annotation.dao;

public interface TestDao {
    public void save();
}

package annotation.dao;

import org.springframework.stereotype.Repository;

@Repository("testDao")
public class TestDaoImpl implements TestDao {
	@Override
	public void save() {
		System.out.println("testDao save");
	}
	
}

package annotation.service;

public interface TestService {
   public void save();
}

package annotation.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import annotation.dao.TestDao;

@Service("testService")
public class TestSeviceImpl implements TestService {
   @Resource(name="testDao")
   private TestDao testDao;
   @Override
   public void save() {
	   testDao.save();
	   System.out.println("testService save");
   }
}

package assemble;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class ComplexUser {
	private String uname;
	private List<String> hobbyList;
	private Map<String,String>residenceMap;
	private Set<String> aliasSet;
	private String[] array;
	public ComplexUser(String uname,List<String> hobbyList
			,Map<String,String> residenceMap
			,Set<String> aliasSet,String[] array) {
		     super();
		     this.uname=uname;
		     this.hobbyList=hobbyList;
		     this.residenceMap=residenceMap;
		     this.aliasSet=aliasSet;
		     this.array=array;
	}
    public ComplexUser(){
    	super();
    }
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public List<String> getHobbyList() {
		return hobbyList;
	}
	public void setHobbyList(List<String> hobbyList) {
		this.hobbyList = hobbyList;
	}
	public Map<String, String> getResidenceMap() {
		return residenceMap;
	}
	public void setResidenceMap(Map<String, String> residenceMap) {
		this.residenceMap = residenceMap;
	}
	public Set<String> getAliasSet() {
		return aliasSet;
	}
	public void setAliasSet(Set<String> aliasSet) {
		this.aliasSet = aliasSet;
	}
	public String[] getArray() {
		return array;
	}
	public void setArray(String[] array) {
		this.array = array;
	}
    @Override
    public String toString() {
    	return "uname="+uname+";hobbyList="+hobbyList+";residenceMap="
    			+residenceMap+";aliasSet="+aliasSet+";array="+array;
    }
} 

package instance;

public class BeanClass {
	public String message;
	public BeanClass() {
		message="构造方法实例化Bean";
	}
	public BeanClass(String s) {
		message=s;
	}
}

package instance;

public class BeanInstanceFactory {
	public BeanClass createBeanClassInstance() {
		return new BeanClass("调用实例工厂方法实例化Bean");
	}
}

package instance;

public class BeanStaticFactory {
	private static BeanClass beanInstance=new BeanClass("调用静态工厂方法");
	public static BeanClass createInstance() {
		return beanInstance;
	}
}

package life;

public class BeanLife {
    public void initMyself() {
    	System.out.println(this.getClass().getName()+"执行自定义的初始方法");
    }
    public void destoryMyself() {
    	System.out.println(this.getClass().getName()+"执行自定义的销毁方法");
    }
}

package test;

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

import annotation.AnnotationUser;
import annotation.controller.TestController;
import assemble.ComplexUser;
import instance.BeanClass;
import life.BeanLife;

public class TestInstance {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
	    BeanClass b1=(BeanClass)appCon.getBean("constructorInstance");
	    System.out.println(b1+b1.message);
	    BeanClass b2=(BeanClass)appCon.getBean("staticFactoryInstance");
	    System.out.println(b2+b2.message);
	    BeanClass b3=(BeanClass)appCon.getBean("staticFactoryInstance");
	    System.out.println(b3+b3.message);
	    */
		/*ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		System.out.println("获得对象前");
		BeanLife blife=(BeanLife)ctx.getBean("beanLife");
		System.out.println("获得对象后"+blife);
		ctx.close();*/
		ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
		 ComplexUser u1=( ComplexUser)appCon.getBean("user1");
		 System.out.println(u1.getUname());
		 ComplexUser u2=( ComplexUser)appCon.getBean("user2");
		 System.out.println(u2.getUname());
		 AnnotationUser au=(AnnotationUser)appCon.getBean("annotationUser");
		 System.out.println(au.getUname());
		 TestController testcon=(TestController)appCon.getBean("testController");
		 testcon.save();
	}

}

<?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
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd"
   xmlns:context="http://www.springframework.org/schema/context"
  >


    <bean id="constructorInstance" class="instance.BeanClass"></bean>
    <bean id="staticFactoryInstance" class="instance.BeanStaticFactory" factory-method="createInstance"></bean>
    <bean id="myFactory" class="instance.BeanInstanceFactory"></bean>    
    <bean id="instanceFactoryInstance" factory-bean="myFactory" factory-method="createBeanClassInstance"></bean>
    <bean id="beanLife" class="life.BeanLife" init-method="initMyself" destroy-method="destoryMyself"></bean>
    <bean id="user1" class="assemble.ComplexUser" >
     <constructor-arg index="0" value="chenheng1"></constructor-arg>
     <constructor-arg index="1">
         <list>
            <value>唱歌</value>
            <value>跳舞</value>
            <value>爬山</value>
         </list>
     </constructor-arg>
     <constructor-arg index="2">
         <map>
           <entry key="dalian" value="大连"></entry>
           <entry key="beijing" value="北京"></entry>
           <entry key="shanghai" value="上海"></entry>
         </map>
     </constructor-arg>
     <constructor-arg index="3">
          <set>
             <value>陈恒100</value>
             <value>陈恒101</value>
             <value>陈恒102010</value>
          </set>
     </constructor-arg>
     <constructor-arg index="4">
          <array>
            <value>aaaaa</value>
            <value>bbbbb</value>
          </array>
     </constructor-arg>
    </bean>
    <bean id="user2" class="assemble.ComplexUser">
       <property name="uname" value="chenheng2"></property>
       <property name="hobbyList">
          <value>kanshu</value>
       </property>
       <property name="residenceMap">
          <map>
             <entry key="shenzhen" value="深圳"></entry>
          </map>
       </property>
       <property name="aliasSet">
          <set>
          <value>陈恒</value>
          </set>
       </property>
       <property name="array">
         <array>
         <value>adsf</value>
         </array>
       </property>
    </bean>
    <context:component-scan base-package="annotation"></context:component-scan>
</beans> 

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号