We are given the first-order linear differential equation:
\[
x \frac{dy}{dx} + 2y = x e^x
\]
Step 1: Rewrite in standard form
First, we rewrite the equation in standard linear form:
\[
\frac{dy}{dx} + \frac{2}{x} y = e^x
\]
Step 2: Find the integrating factor
The integrating factor \( \mu(x) \) is given by:
\[
\mu(x) = e^{\int P(x) \, dx}
\]
where \( P(x) = \frac{2}{x} \).
Thus, the integrating factor is:
\[
\mu(x) = e^{\int \frac{2}{x} \, dx} = e^{2 \log x} = x^2
\]
Step 3: Conclusion
Thus, the integrating factor is \( x^2 \).
Thus, the correct answer is option (D), \( x^2 \).