spring第二天学习总结——spring配置文件

1.set的XML实现
新建一个Person类,Person类的属性包括以下

private Long pid;
    private String name;
    private Student student;
    private List lists;
    private Set sets;
    private Map map;
    private Properties properties;
    private Object[] object;

提供各自的get set方法,其中Student是自己设置的一个类。加一个say测试方法

public class Student {
    public void say() {
        System.out.println("Student.......");
    }
}

接下来就是在配置文件中配置各种类型的属性:
applicationContext.xml

<bean id = "person" class="com.my.spring.di.xml.setter.Person">
        <!-- property 就是bean 的一个属性
                name 用来描述属性的名称,
                value 就是值 如果是一般类型(包括基本类型和String类型)
         -->
        <property name="pid" value="1"></property>
        <property name="name" value="狗蛋"></property>
        <!-- spring容器内部创建的student对象给Person的student属性赋值 -->
        <property name="student" ref="student"></property>

        <property name="lists">
            <list>
  <!--普通类型的包括String类型,直接用<value>赋值-->
                <value>list1</value>
                <value>list2</value>
 <!-- 对象类型用<ref bean  = '对象id'> 赋值-->
                <ref bean="student"/>
            </list>
        </property>
        <property name="sets">
            <set>
                <value>set1</value>
                <value>set2</value>
                <ref bean="student"/>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="m1">
                    <value>map1</value>
                </entry>
                <entry key="m1">
                    <ref bean = "student"/>
                </entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="p1">prop1</prop>
                <prop key="p2">prop2</prop>
            </props>
        </property>
        <property name="objects">
            <list>
                <value>obj1</value>
                <ref bean = "student"/>
            </list>
        </property>
    </bean>


    <bean id = "student" class="com.my.spring.di.xml.setter.Student"></bean>

测试类:

public class SetterText {
    public void textDi_XMl_Setter() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) applicationContext.getBean("person");
        System.out.println("Pid = "+person.getPid());
        System.out.println("name = "+person.getName());
        System.out.println("list = "+person.getLists().size());
    }
    public static void main(String[] args) {
        SetterText text = new SetterText();
        text.textDi_XMl_Setter();
    }
}

2.构造函数的XML实现
依旧运用第一条的Person类做测试,这次我们只提供各属性的get方法,获取属性时候用到,并且提供不同参数的构造函数。假如我们提供以下两个构造函数

public Person(String name) {
        // TODO Auto-generated constructor stub
        this.name = name;
    }
    public Person(String name, Student student) {
        // TODO Auto-generated constructor stub
        this.name = name;
        this.student = student;
    }

配置文件(applicatsionContext.xm)中:

<bean id = "personContructor" class="com.my.spring.di.xml.contructor.Person">
        <!--constructor-arg:指的是peroson这个类指定的构造器 的参数 
            index:下标,从0 开始
            Value:如果一般类型用value赋值
            ref:引用类型赋值
        -->
        <constructor-arg index="0" value="yss"></constructor-arg>
        <constructor-arg index="1" ref="constructorStudent"></constructor-arg>
    </bean>
    <bean id = "constructorStudent" class="com.my.spring.di.xml.contructor.Student"></bean>

测试类:

public class ContructorText {
    public void textDi_XMl_Contructor() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person) applicationContext.getBean("personContructor");
        System.out.println(person.getName());
        person.getStudent().say();
    }
    public static void main(String[] args) {
        ContructorText text = new ContructorText();
        text.textDi_XMl_Contructor();
    }
}

3.一个简单的MVC实现 (主要注意结构,和以往的结构有什么不同)
1.首先建立一个Dao包,com.my.spring.person.dao
在该包里创建一个PersonDao接口,创建savePerson方法;

public interface PersonDao {
    public void savePerson();
}

2.建立一个DaoImpl包,com.my.spring.person.dao.impl
建立一个类PersonDaoImol。实现savePerson接口,重写saveStudent()方法(这里不具体实现数据库操作了,简单输出一下savaStudent….即可)

public class PersonDaoImpl implements PersonDao{

    @Override
    public void savePerson() {
        // TODO Auto-generated method stub
        System.out.println("save Person ..........");
    }

}

3.创建一个Serviece包,com.my.spring.person.serveice
在该包内创建PersonService接口,创建savePerson方法;

public interface PersonService {
    public void savePerson();
}

4.创建ServiceImpl包,com.my.spring.person.serviceimpl
在该包里创建PersonServiceImpl类,实现PersonService,
添加PersonDao类的一个对象,并提供get set方法,重写saveStudent()方法

public class PsersonServiceImpl implements PersonService{

    private PersonDao personDao;
    public PersonDao getPersonDao() {
        return personDao;
    }

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    @Override
    public void savePerson() {
        // TODO Auto-generated method stub
        this.personDao.savePerson();
    }
}

5.创建安Action包,com.my.spring.person.action
创建PersonAction接口,添加saceStudent方法
6.创建ActionImpl包,com.my.spring.person.action.impl
创建personActionImpl类,实现PersonAction接口,重写saveStudent方法,添加 PersonService 对象,提供get Set方法

public class PersonActionImpl {
    private PersonService personService;

    public PersonService getPersonService() {
        return personService;
    }
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }
    public void savePerson() {
        this.personService.savePerson();
    }
}

7.创建测试类

public class MVCText {
    public void  textMVC() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonAction personAction = (PersonAction) applicationContext.getBean("personAction");
        personAction.savePerson();
    }
    public static void main(String[] args) {
        MVCText text = new MVCText();
        text.textMVC();
    }
}

8applicationContext.xml配置

<bean id = "personDao" class="com.my.spring.person.dao.impl.PersonDaoImpl"></bean>
    <bean id = "personServic" class="com.my.spring.person.service.impl.PsersonServiceImpl">
        <property name="personDao">
            <ref bean="personDao"/>
        </property>
    </bean>
    <bean id="personAction" class="com.my.spring.person.action.PersonAction">
        <property name="personService">
            <ref bean="personServic"/>
        </property>
    </bean>

以上就是一个MVC实现的大体实现,看懂大体的结构即可,项目中的实现绝非如此简单。还需继续努力学习。

4.spring的extend
spring中也有继承
例如我们创建一个类Person。

public class Person {
    private String name;

    public String getName() {
        return name;
    }

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

}

再建立一个Student继承了Person类

public class Student extends Person{

}

这样Student类中就有了Person类的属性name
怎样在配置文件中配置Student类的name属性呢,有两种方法可以实现,首先看第一种方法
1.在父类中进行赋值,子类通过parent继承父类的内容

<bean id = "extendsPerson" class = "com.my.spring.extend.Person">
        <property name="name" value="aaa"></property>
    </bean>
    <!-- parent实现了spring容器内部的继承关系 -->
    <bean id = "extendsStudent" class="com.my.spring.extend.Student" parent="extendsPerson"></bean>

第二种方法
因为java的继承机制,子类继承了父类的setXXX()方法,所以子类可以使用setXXX()方法设值

<bean id = "extendsStudent2" class="com.my.spring.extend.Student">
        <property name="name" value="yss2"></property>
</bean>

以上两种方法都可以实现继承

猜你喜欢

转载自blog.csdn.net/qq_39411208/article/details/81535851