Maven创建web项目,Spring-Ioc

2020-02-24  本文已影响0人  煗NUAN

一.Maven常见命令

二.Maven依赖

图片0.jpg

三.创建Maven的web项目

1.ideal创建web项目

图片1.jpg
    <build>
        <plugins>
        <!-- define the project compile level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- 添加tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <port>8080</port>
                </configuration>
            </plugin>
        </plugins>
    </build>
图片2.jpg

2.eclipse创建web项目

图片3.jpg 图片4.jpg
<build>
        <plugins>
            <!-- define the project compile level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- 添加tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <port>8080</port>
                </configuration>
            </plugin>
        </plugins>
    </build>
图片5.jpg 图片6.jpg

四.Spring-IoC

1.Spring的好处

2.scope的属性

3.小结

五.Spring-IoC的简单例题

创建⼀个学⽣类,⾥⾯有如下属性:
1. ⽣⽇BirthDays类⾥⾯的属性有year int, month int, day int
2. Student 类:sid int, name String, sex boolean, score double, tel String,BirthDay birth
3. 使⽤⽆参构造器和property给对象赋值
4. 使⽤带参构造器给对象赋值(四种⽅式都均使⽤)
5. 使⽤namespace命名空间p给对象学⽣赋值
public class Student {
    private int sid;
    private String name;
    private boolean sex;
    private double score;
    private String tel;
    private BirthDays birth;

    public Student() {
    }

    public Student(int sid, String name, boolean sex, double score, String tel, BirthDays birth) {
        this.sid = sid;
        this.name = name;
        this.sex = sex;
        this.score = score;
        this.tel = tel;
        this.birth = birth;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

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

    public boolean isSex() {
        return sex;
    }

    public void setSex(boolean sex) {
        this.sex = sex;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public BirthDays getBirth() {
        return birth;
    }

    public void setBirth(BirthDays birth) {
        this.birth = birth;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Student{");
        sb.append("sid=").append(sid);
        sb.append(", name='").append(name).append('\'');
        sb.append(", sex=").append(sex ? "男":"女");
        sb.append(", score=").append(score);
        sb.append(", tel='").append(tel).append('\'');
        sb.append(", birth=").append(birth);
        sb.append('}');
        return sb.toString();
    }
}
public class BirthDays {
    private int year;
    private int month;
    private int day;

    public BirthDays() {
    }

    public BirthDays(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("BirthDays{");
        sb.append("year=").append(year);
        sb.append(", month=").append(month);
        sb.append(", day=").append(day);
        sb.append('}');
        return sb.toString();
    }
}
图片7.jpg
<!--student.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="birth1" class="com.mvn.bean.BirthDays">
        <property name="year" value="1998" />
        <property name="month" value="8" />
        <property name="day" value="18" />
    </bean>
    <bean id="stu1" class="com.mvn.bean.Student">
        <property name="sid" value="21" />
        <property name="name" value="大毛" />
        <property name="sex" value="true" />
        <property name="tel" value="18815527289" />
        <property name="score" value="89"/>
        <property name="birth" ref="birth1" />  <!--对象赋值的话使用ref-->
    </bean>

    <!--使用带参构造器 name+value-->
    <bean id="birth2" class="com.mvn.bean.BirthDays">
        <constructor-arg name="year" value="1998" />
        <constructor-arg name="month" value="8" />
        <constructor-arg name="day" value="18" />
    </bean>
    <bean id="stu2" class="com.mvn.bean.Student">
        <constructor-arg name="sid" value="21" />
        <constructor-arg name="name" value="大毛" />
        <constructor-arg name="sex" value="true" />
        <constructor-arg name="tel" value="18815527289" />
        <constructor-arg name="score" value="89"/>
        <constructor-arg name="birth" ref="birth1" />
    </bean>

    <!--使用带参构造器 index+value-->
    <bean id="birth3" class="com.mvn.bean.BirthDays">
        <constructor-arg index="0" value="1998" />
        <constructor-arg index="1" value="8" />
        <constructor-arg index="2" value="18" />
    </bean>
    <bean id="stu3" class="com.mvn.bean.Student">
        <constructor-arg index="0" value="21" />
        <constructor-arg index="1" value="大毛" />
        <constructor-arg index="2" value="true" />
        <constructor-arg index="3" value="89"/>
        <constructor-arg index="4" value="18815527289" />
        <constructor-arg index="5" ref="birth1" />
    </bean>

    <!--使用带参构造器 index+value-->
    <bean id="birth4" class="com.mvn.bean.BirthDays">
        <constructor-arg type="int" value="1998" />
        <constructor-arg type="int" value="8" />
        <constructor-arg type="int" value="18" />
    </bean>
    <bean id="stu4" class="com.mvn.bean.Student">
        <constructor-arg type="int" value="21" />
        <constructor-arg type="java.lang.String" value="大毛" />
        <constructor-arg type="boolean" value="true" />
        <constructor-arg type="double" value="89"/>
        <constructor-arg type="java.lang.String" value="18815527289" />
        <constructor-arg type="com.mvn.bean.BirthDays" ref="birth1" />
    </bean>

    <!--使用带参构造器  value-->
    <bean id="birth5" class="com.mvn.bean.BirthDays">
        <constructor-arg type="int" value="1998" />
        <constructor-arg type="int" value="8" />
        <constructor-arg type="int" value="18" />
    </bean>
    <bean id="stu5" class="com.mvn.bean.Student">
        <constructor-arg  value="21" />
        <constructor-arg  value="大毛" />
        <constructor-arg  value="true" />
        <constructor-arg  value="89"/>
        <constructor-arg  value="18815527289" />
        <constructor-arg  ref="birth1" />
    </bean>
</beans>

<!--student1.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:p="http://www.springframework.org/schema/p"
       <!--需要添加xmlns:p="http://www.springframework.org/schema/p"代码才能生效-->
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--使用namespace命名空间p给对象学⽣赋值-->
    <bean id="birth" class="com.mvn.bean.BirthDays" p:year="1997" p:month="8" p:day="15" />
    <bean id="stu" class="com.mvn.bean.Student" p:sid="33" p:name="小毛" p:sex="true" p:score="98" p:tel="18734561111" p:birth-ref="birth"/>

</beans>
public class Student1 {
    @Test
    public void student1(){

        ApplicationContext ac=new ClassPathXmlApplicationContext("student1.xml");

        System.out.println(ac.getBean("birth"));
        System.out.println(ac.getBean("stu"));
    }
}


public class StudentTest {
    @Test
    public void studentTest(){
        ApplicationContext ac=new ClassPathXmlApplicationContext("student.xml");
        //无参构造器
        System.out.println(ac.getBean("birth1"));
        System.out.println(ac.getBean("stu1"));

        //有参构造器name+value
        System.out.println(ac.getBean("birth2"));
        System.out.println(ac.getBean("stu2"));

        //有参构造器index+value
        System.out.println(ac.getBean("birth3"));
        System.out.println(ac.getBean("stu3"));

        //有参构造器type+value
        System.out.println(ac.getBean("birth4"));
        System.out.println(ac.getBean("stu4"));

        //有参构造器type+value
        System.out.println(ac.getBean("birth5"));
        System.out.println(ac.getBean("stu5"));
    }
}
上一篇 下一篇

猜你喜欢

热点阅读